<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("https://google.com"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="问题,">










<meta name="description" content="emojiutf8 编码问题1234567891011  function nickname_encode($s)&amp;#123;        $chars = preg_split(&apos;//u&apos;, $s, null, PREG_SPLIT_NO_EMPTY);        foreach ($chars as &amp;amp;$c) &amp;#123;            if (strlen($c) &amp;g">
<meta name="keywords" content="问题">
<meta property="og:type" content="article">
<meta property="og:title" content="开发中遇到的问题整理">
<meta property="og:url" content="http://yoursite.com/2018/12/04/开发中遇到的问题整理/index.html">
<meta property="og:site_name" content="苏生不惑的博客">
<meta property="og:description" content="emojiutf8 编码问题1234567891011  function nickname_encode($s)&amp;#123;        $chars = preg_split(&apos;//u&apos;, $s, null, PREG_SPLIT_NO_EMPTY);        foreach ($chars as &amp;amp;$c) &amp;#123;            if (strlen($c) &amp;g">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://segmentfault.com/img/bVbfU5W">
<meta property="og:image" content="https://segmentfault.com/img/bVbfVFU">
<meta property="og:image" content="https://segmentfault.com/img/bVbgb3d">
<meta property="og:image" content="https://segmentfault.com/img/bVbgxJQ?w=782&h=387">
<meta property="og:updated_time" content="2019-11-15T01:53:49.229Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="开发中遇到的问题整理">
<meta name="twitter:description" content="emojiutf8 编码问题1234567891011  function nickname_encode($s)&amp;#123;        $chars = preg_split(&apos;//u&apos;, $s, null, PREG_SPLIT_NO_EMPTY);        foreach ($chars as &amp;amp;$c) &amp;#123;            if (strlen($c) &amp;g">
<meta name="twitter:image" content="https://segmentfault.com/img/bVbfU5W">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/12/04/开发中遇到的问题整理/">



<meta name="referrer" content="never"> ​​​​


  <title>开发中遇到的问题整理 | 苏生不惑的博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">苏生不惑的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/12/04/开发中遇到的问题整理/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">开发中遇到的问题整理</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-12-04T20:12:36+08:00">
                2018-12-04
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  21.3k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  107 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="emoji"><a href="#emoji" class="headerlink" title="emoji"></a>emoji</h3><p><a href="https://laravel-china.org/topics/13864/remember-never-use-utf8-encoding-in-mysql" target="_blank" rel="noopener">utf8 编码问题</a><br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">nickname_encode</span>(<span class="params">$s</span>)</span>&#123;</span><br><span class="line">        $chars = preg_split(<span class="string">'//u'</span>, $s, <span class="literal">null</span>, PREG_SPLIT_NO_EMPTY);</span><br><span class="line">        foreach ($chars <span class="keyword">as</span> &amp;$c) &#123;</span><br><span class="line">            <span class="keyword">if</span> (strlen($c) &gt; <span class="number">3</span> || $c === <span class="string">"%"</span>)&#123;</span><br><span class="line">                $c = urlencode($c);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> implode($chars);</span><br><span class="line">    &#125;</span><br><span class="line">解码直接用urldecode就行了。</span><br></pre></td></tr></table></figure></p>
<h3 id="strtotime"><a href="#strtotime" class="headerlink" title="strtotime"></a>strtotime</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"2017-06-31"</span>)));</span><br><span class="line"><span class="comment">//输出2017-07-01</span></span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"-1 month"</span>, strtotime(<span class="string">"2017-03-31"</span>))));</span><br><span class="line"><span class="comment">//输出2017-03-03</span></span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"+1 month"</span>, strtotime(<span class="string">"2017-08-31"</span>))));</span><br><span class="line"><span class="comment">//输出2017-10-01</span></span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"next month"</span>, strtotime(<span class="string">"2017-01-31"</span>))));</span><br><span class="line"><span class="comment">//输出2017-03-03</span></span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"last month"</span>, strtotime(<span class="string">"2017-03-31"</span>))));</span><br><span class="line"><span class="comment">//输出2017-03-03</span></span><br><span class="line"></span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"last day of -1 month"</span>, strtotime(<span class="string">"2017-03-31"</span>))));</span><br><span class="line"><span class="comment">//输出2017-02-28</span></span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"first day of +1 month"</span>, strtotime(<span class="string">"2017-08-31"</span>))));</span><br><span class="line"><span class="comment">////输出2017-09-01</span></span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"first day of next month"</span>, strtotime(<span class="string">"2017-01-31"</span>))));</span><br><span class="line"><span class="comment">////输出2017-02-01</span></span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"last day of last month"</span>, strtotime(<span class="string">"2017-03-31"</span>))));</span><br><span class="line"><span class="comment">////输出2017-02-28</span></span><br><span class="line"></span><br><span class="line">需要获得多少天之前 $day </span><br><span class="line"></span><br><span class="line">mktime(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, date(<span class="string">"m"</span>, $startTime), date(<span class="string">"d"</span>, $startTime) - $day, date(<span class="string">"Y"</span>, $startTime));</span><br><span class="line"></span><br><span class="line"> 这个坑在carbon中一样的有. 比如:Carbon::now()-&gt;subMonth(<span class="number">1</span>)-&gt;startOfMonth() 一样的会出现上面说的问题. 你必须这样写:Carbon::now()-&gt;startOfMonth()-&gt;subMonth(<span class="number">1</span>)</span><br><span class="line"> Carbon::now()-&gt;subMonthNoOverflow(<span class="number">1</span>)-&gt;startOfMonth()</span><br><span class="line">https:<span class="comment">//laravel-china.org/articles/17317</span></span><br><span class="line"></span><br><span class="line">$i = <span class="number">12</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>($i &gt;= <span class="number">1</span>) &#123;</span><br><span class="line">    echo date(<span class="string">'Y-m'</span>, strtotime(<span class="string">'-'</span> . $i . <span class="string">' month'</span>, strtotime(<span class="string">'2018-8'</span>))) . <span class="string">"\n"</span>;</span><br><span class="line">    $i--;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//laravel-china.org/articles/18175?#reply67278</span></span><br></pre></td></tr></table></figure>
<p><a href="https://laravel-china.org/topics/15192/puzzling-strtotime-bird-brother" target="_blank" rel="noopener">解决方法</a></p>
<h3 id="三元运算符"><a href="#三元运算符" class="headerlink" title="三元运算符"></a>三元运算符</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">运算符可 用于判断$a变量不存在的情况（也可用于数组），而使用三元运算符判断一个未定义的变量，PHP会抛出异常。也正是因为这样，用??判断一个赋值为<span class="number">0</span>的变量的时候结果是不一样的。https:<span class="comment">//laravel-china.org/articles/17304?#reply64712</span></span><br><span class="line">$a=<span class="number">0</span>;</span><br><span class="line">$c=<span class="number">1</span>;</span><br><span class="line">$b=$a??$c;</span><br><span class="line">echo <span class="string">'a:'</span>.$a.<span class="string">',b:'</span>.$b.<span class="string">',c:'</span>.$c;</span><br><span class="line"><span class="comment">//a:0,b:0,c:1</span></span><br><span class="line">$a=<span class="number">0</span>;</span><br><span class="line">$c=<span class="number">1</span>;</span><br><span class="line">$b=$a?$a:$c;</span><br><span class="line">echo <span class="string">'a:'</span>.$a.<span class="string">',b:'</span>.$b.<span class="string">',c:'</span>.$c;</span><br><span class="line"><span class="comment">//a:0,b:1,c:1</span></span><br></pre></td></tr></table></figure>
<h3 id="Error-while-reading-line-from-the-server-tcp-127-0-0-1-6379"><a href="#Error-while-reading-line-from-the-server-tcp-127-0-0-1-6379" class="headerlink" title="Error while reading line from the server. [tcp://127.0.0.1:6379]"></a>Error while reading line from the server. [tcp://127.0.0.1:6379]</h3><p>redis 加了代理 Twemproxy，而 predis 对这个配置默认执行 select 操作，导致了连接错误。</p>
<p>大家以后要注意，如果 redis 有代理的话，别忘了把  <code>&#39;database&#39; =&gt; 0,</code>这个配置删掉。<br><a href="https://laravel-china.org/topics/6774/meet-a-pit-of-laravel-redis-share" target="_blank" rel="noopener">https://laravel-china.org/topics/6774/meet-a-pit-of-laravel-redis-share</a></p>
<h3 id="PHP-empty-函数判断结果为空"><a href="#PHP-empty-函数判断结果为空" class="headerlink" title="PHP empty 函数判断结果为空"></a>PHP empty 函数判断结果为空</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">person</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $attributes = [];</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">array $attributes</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;attributes = $attributes;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__get</span>(<span class="params">$name</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;attributes[$name] ?? <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//laravel-china.org/articles/12530/the-shock-php-empty-function-is-empty-but-the-actual-value-is-nonempty</span></span><br><span class="line"></span><br><span class="line">https:<span class="comment">//laravel-china.org/topics/3021/isset-is-not-right-after-upgrading-php7</span></span><br><span class="line">var_dump(</span><br><span class="line">    $person-&gt;firstName,</span><br><span class="line">    empty($person-&gt;firstName),</span><br><span class="line">    isset($person-&gt;firstName),</span><br><span class="line">    is_null($person-&gt;firstName)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-Redis-多个进程同时取队列问题"><a href="#Laravel-Redis-多个进程同时取队列问题" class="headerlink" title="Laravel Redis 多个进程同时取队列问题"></a>Laravel Redis 多个进程同时取队列问题</h3><p>Laravel 使用 Redis 的 list 作为队列的数据结构，并会为每个队列分配一个 ID<br>Laravel 中入队列方法<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"> </span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">pushRaw</span>(<span class="params">$payload, $queue = null, array $options = []</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> $<span class="keyword">this</span>-&gt;getConnection()-&gt;rpush($<span class="keyword">this</span>-&gt;getQueue($queue), $payload);</span><br><span class="line">  </span><br><span class="line"> <span class="keyword">return</span> Arr::get(json_decode($payload, <span class="literal">true</span>), <span class="string">'id'</span>);</span><br><span class="line">&#125;</span><br><span class="line">用的是 Redis 的 rpush 命令。</span><br><span class="line"></span><br><span class="line">Laravel 中取队列方法</span><br><span class="line"> </span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">pop</span>(<span class="params">$queue = null</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> $original = $queue ?: $<span class="keyword">this</span>-&gt;<span class="keyword">default</span>; </span><br><span class="line"> $queue = $<span class="keyword">this</span>-&gt;getQueue($queue); </span><br><span class="line"> $<span class="keyword">this</span>-&gt;migrateExpiredJobs($queue.<span class="string">':delayed'</span>, $queue); </span><br><span class="line"> <span class="keyword">if</span> (! is_null($<span class="keyword">this</span>-&gt;expire)) &#123;</span><br><span class="line">  $<span class="keyword">this</span>-&gt;migrateExpiredJobs($queue.<span class="string">':reserved'</span>, $queue);</span><br><span class="line"> &#125; </span><br><span class="line"> list($job, $reserved) = $<span class="keyword">this</span>-&gt;getConnection()-&gt;<span class="built_in">eval</span>(</span><br><span class="line">  LuaScripts::pop(), <span class="number">2</span>, $queue, $queue.<span class="string">':reserved'</span>, $<span class="keyword">this</span>-&gt;getTime() + $<span class="keyword">this</span>-&gt;expire</span><br><span class="line"> ); </span><br><span class="line"> <span class="keyword">if</span> ($reserved) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> RedisJob($<span class="keyword">this</span>-&gt;container, $<span class="keyword">this</span>, $job, $reserved, $original);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br><span class="line">这里用的是 lua 脚本取队列，如下：</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">pop</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> <span class="keyword">return</span> &lt;&lt;&lt;'LUA'</span><br><span class="line">local job = redis.call('lpop', KEYS[1])</span><br><span class="line">local reserved = false</span><br><span class="line">if(job ~= false) then</span><br><span class="line">reserved = cjson.decode(job)</span><br><span class="line">reserved['attempts'] = reserved['attempts'] + 1</span><br><span class="line">reserved = cjson.encode(reserved)</span><br><span class="line">redis.call('zadd', KEYS[2], ARGV[1], reserved)</span><br><span class="line">end</span><br><span class="line">return &#123;job, reserved&#125;</span><br><span class="line">LUA;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>那么结论是：从 Laravel 的处理方式和打印的日志结果看，即使多个进程读取同一个队列，也不会读取到一样的数据。</p>
<h3 id="队列执行失败之后根本不会执行failed方法"><a href="#队列执行失败之后根本不会执行failed方法" class="headerlink" title="队列执行失败之后根本不会执行failed方法"></a>队列执行失败之后根本不会执行failed方法</h3><p>执行队列侦听器时加上 –tries参数即可，failed()方法只有在重试指定次数完成后才会调用failed()方法如：<br> <a href="http://blog.zhouchenxi.cn/post/51.html" target="_blank" rel="noopener">http://blog.zhouchenxi.cn/post/51.html</a><br>php artisan queue:listen –queue=default,listeners –tries=3<br>顺便提一下：如果指定了队列名称必须在侦听器的参数上加上  –queue参数才行，不然没有指定的队列是不会运行的。<br>event 指定队列<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">  public $queue = <span class="string">'listeners'</span>;</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">queue</span>(<span class="params">$queue, $job, $data</span>)</span></span><br><span class="line"><span class="function">       </span>&#123;</span><br><span class="line">              <span class="keyword">if</span> (isset($<span class="keyword">this</span>-&gt;queue, $<span class="keyword">this</span>-&gt;delay)) &#123;</span><br><span class="line">                     <span class="keyword">return</span> $queue-&gt;laterOn($<span class="keyword">this</span>-&gt;queue, $<span class="keyword">this</span>-&gt;delay, $job, $data);</span><br><span class="line">              &#125;</span><br><span class="line"></span><br><span class="line">              <span class="keyword">if</span> (isset($<span class="keyword">this</span>-&gt;queue)) &#123;</span><br><span class="line">                     <span class="keyword">return</span> $queue-&gt;pushOn($<span class="keyword">this</span>-&gt;queue, $job, $data);</span><br><span class="line">              &#125;</span><br><span class="line"></span><br><span class="line">              <span class="keyword">if</span> (isset($<span class="keyword">this</span>-&gt;delay)) &#123;</span><br><span class="line">                     <span class="keyword">return</span> $queue-&gt;later($<span class="keyword">this</span>-&gt;delay, $job, $data);</span><br><span class="line">              &#125;</span><br><span class="line"></span><br><span class="line">              <span class="keyword">return</span> $queue-&gt;push($job, $data);</span><br><span class="line">       &#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="php7不支持-preg-replace-e修正符"><a href="#php7不支持-preg-replace-e修正符" class="headerlink" title="php7不支持 preg_replace e修正符"></a>php7不支持 preg_replace e修正符</h3><p>车轮升级PHP7踩过的一些坑 <a href="http://php.swoole.com/wiki/%E8%BD%A6%E8%BD%AE%E5%8D%87%E7%BA%A7PHP7%E8%B8%A9%E8%BF%87%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91" target="_blank" rel="noopener">http://php.swoole.com/wiki/%E8%BD%A6%E8%BD%AE%E5%8D%87%E7%BA%A7PHP7%E8%B8%A9%E8%BF%87%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91</a></p>
<p>php5 php7不兼容的地方：<a href="https://segmentfault.com/a/1190000013837897" target="_blank" rel="noopener">https://segmentfault.com/a/1190000013837897</a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$joinStr = preg_replace(<span class="string">"/__([A-Z_-]+)__/e"</span>,$prex.<span class="string">".strtolower('$1')"</span>,$joinStr);</span><br><span class="line"><span class="comment">//替换为</span></span><br><span class="line">preg_replace_callback(<span class="string">"/__([A-Z_-]+)__/"</span>,<span class="function"><span class="keyword">function</span>(<span class="params">$r</span>) <span class="title">use</span> (<span class="params">&amp;$joinStr</span>)</span>&#123;</span><br><span class="line">    $joinStr = $prex.strtolower($r[<span class="number">1</span>]);</span><br><span class="line">&#125;,$joinStr);</span><br></pre></td></tr></table></figure>
<h3 id="内存溢出"><a href="#内存溢出" class="headerlink" title="内存溢出"></a>内存溢出</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://laravel-china.org/articles/16312/handling-large-amounts-of-data-to-write-files-to-avoid-memory-overflow-and-response-timeout</span></span><br><span class="line">$beginIndex = <span class="number">0</span>;</span><br><span class="line">    $pageLimit = <span class="number">50000</span>;</span><br><span class="line">    $endIndex = $beginIndex + $pageLimit;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> ($pageData = \DB::connection(<span class="string">'hub_log'</span>)-&gt;select(<span class="string">"SELECT * FROM `&#123;$tableName&#125;` WHERE id &gt; &#123;$beginIndex&#125; and id &lt;= &#123;$endIndex&#125;"</span>)) &#123;</span><br><span class="line"></span><br><span class="line">        foreach ($pageData <span class="keyword">as</span> $row) &#123;</span><br><span class="line">            file_put_contents(storage_path(<span class="string">'mcd_rawdata/'</span>.$tableName.<span class="string">'.txt'</span>), $row-&gt;Content . <span class="string">"\n"</span>, FILE_APPEND);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $beginIndex += $pageLimit;</span><br><span class="line">        $endIndex += $pageLimit;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    $public_path = $time.<span class="string">'test.txt'</span>;</span><br><span class="line"></span><br><span class="line">    DB::table(<span class="string">'20171220'</span>)-&gt;orderBy(<span class="string">'id'</span>)-&gt;chunk(<span class="number">10000</span>,<span class="function"><span class="keyword">function</span>(<span class="params">$data</span>) <span class="title">use</span> (<span class="params">$public_path</span>)</span>&#123;</span><br><span class="line">        $arr = <span class="string">''</span>;</span><br><span class="line">        foreach ($data-&gt;toArray() <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">            $arr .= $value-&gt;Content.<span class="string">"\n"</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        file_put_contents(public_path($public_path),$arr, FILE_APPEND);</span><br><span class="line">        unset($data);unset($arr);</span><br><span class="line">    &#125;);</span><br></pre></td></tr></table></figure>
<h3 id="git-bash-输入python命令停滞"><a href="#git-bash-输入python命令停滞" class="headerlink" title="git bash 输入python命令停滞"></a>git bash 输入python命令停滞</h3><p>$ python -i<br>$ winpty python -3</p>
<h3 id="保留两位小数"><a href="#保留两位小数" class="headerlink" title="保留两位小数"></a>保留两位小数</h3><p> round(a,2)<br>r = lambda f: f - f % 0.01<br>int(a*100)/100<br>print(“%.2f” %a)</p>
<h3 id="后期绑定"><a href="#后期绑定" class="headerlink" title="后期绑定"></a>后期绑定</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://www.v2ex.com/t/482659#reply8</span></span><br><span class="line">def testFun():</span><br><span class="line">    <span class="keyword">return</span>(lambda x : i*x <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">4</span>))</span><br><span class="line"><span class="keyword">for</span> everyLambda <span class="keyword">in</span> testFun():</span><br><span class="line">    print(everyLambda(<span class="number">2</span>))</span><br><span class="line">&amp; python test.py</span><br><span class="line"><span class="number">0</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="number">4</span></span><br><span class="line"><span class="number">6</span></span><br><span class="line">def testFun():</span><br><span class="line">    temp = [lambda x : i*x <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">4</span>)]</span><br><span class="line">    <span class="keyword">return</span> temp</span><br><span class="line"><span class="keyword">for</span> everyLambda <span class="keyword">in</span> testFun():</span><br><span class="line">    print(everyLambda(<span class="number">2</span>))</span><br><span class="line">&amp; python test.py</span><br><span class="line"><span class="number">6</span></span><br><span class="line"><span class="number">6</span></span><br><span class="line"><span class="number">6</span></span><br><span class="line"><span class="number">6</span></span><br><span class="line">第一个是括号(), 为生成器, 返回 generator</span><br><span class="line">第二个是中括号[], 为列表生成式, 返回数组 列表表达式是即时计算的, 而生成器是迭代时才会计算</span><br></pre></td></tr></table></figure>
<h3 id=""><a href="#" class="headerlink" title="{} + []"></a>{} + []</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">(&#123;&#125;).valueOf() <span class="comment">// []</span></span><br><span class="line"><span class="comment">// 返回来的是个对象,继续调用 toString https://www.v2ex.com/t/480998#reply11</span></span><br><span class="line">[].toString() <span class="comment">// ""</span></span><br><span class="line"></span><br><span class="line">[] + <span class="number">1</span><span class="comment">// "1"</span></span><br><span class="line">[] + <span class="string">'a'</span> <span class="comment">// "a"</span></span><br></pre></td></tr></table></figure>
<h3 id="echo-后发送-header"><a href="#echo-后发送-header" class="headerlink" title="echo 后发送 header"></a>echo 后发送 header</h3><p>使用 fastcgi_finish_request();（ PHP-fpm ）可以强制断开客户端连接 session 本质上是 header set-cookie，所以不算输出。这里的输出指的是 response body 的部分<br> <a href="https://www.v2ex.com/t/481127#reply16" target="_blank" rel="noopener">https://www.v2ex.com/t/481127#reply16</a><br><img src="https://segmentfault.com/img/bVbfU5W" alt="clipboard.png"></p>
<h3 id="isset在php5-6-和php7-0-的一些差异"><a href="#isset在php5-6-和php7-0-的一些差异" class="headerlink" title="isset在php5.6-和php7.0+的一些差异"></a>isset在php5.6-和php7.0+的一些差异</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//segmentfault.com/a/1190000016097997</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ProductCategory</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">const</span> TYPES = [</span><br><span class="line">        <span class="number">1</span> =&gt; <span class="string">'type1'</span>,</span><br><span class="line">        <span class="number">2</span> =&gt; <span class="string">'type2'</span>,  </span><br><span class="line">    ];</span><br><span class="line">    </span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getType</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> isset(self::TYPES[$<span class="keyword">this</span>-&gt;type]) ? self:TYPES[$<span class="keyword">this</span>-&gt;type] : <span class="string">'unrecognized_type'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//把汉字拆分为数组</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">ch2arr</span>(<span class="params">$str</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> preg_split(<span class="string">'//u'</span>, $str, <span class="literal">null</span>, PREG_SPLIT_NO_EMPTY);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="laravel-中使用-导出-excel-时报错"><a href="#laravel-中使用-导出-excel-时报错" class="headerlink" title="laravel 中使用 导出 excel 时报错"></a>laravel 中使用 导出 excel 时报错</h3><p><code>PHPExcel_Calculation_Exception: Q5!A65 → Formula Error: An unexpected error occured in /application/www/web_git-pull/vendor/phpoffice/phpexcel/Classes/PHPExcel/Cell.php:291</code></p>
<p>原因：</p>
<p>在excel中一个单元格如果是以<code>=</code>开头，则说明这个单元格是根据其他单元格的值算出来的，<code>=</code>后面必须跟着一个合法的表达式,而那个字符串是用户的输入，很明显不应该是一个合法的表达式，所以应该在代码中过滤掉</p>
<p>解决：<br><code>$str = &quot;\t&quot;.$str;</code>//同时解决 如 11111111111 显示成 1.11111E+29<br>参考<br><a href="https://github.com/Maatwebsite/Laravel-Excel/issues/506" target="_blank" rel="noopener">https://github.com/Maatwebsite/Laravel-Excel/issues/506</a><br><a href="https://stackoverflow.com/questions/11189145/formula-error-in-phpexcel" target="_blank" rel="noopener">https://stackoverflow.com/questions/11189145/formula-error-in-phpexcel</a></p>
<h3 id="gzip-UnicodeDecodeError"><a href="#gzip-UnicodeDecodeError" class="headerlink" title="gzip UnicodeDecodeError"></a>gzip UnicodeDecodeError</h3><p><a href="https://mp.weixin.qq.com/s?__biz=MzI2OTQyMDEzNQ==&amp;mid=2247483805&amp;idx=1&amp;sn=64fb8e61f049b3354f83bbfa3ae72f41&amp;chksm=eae1d295dd965b83b5a2052daf9c1e3ae0e63e663a102f1a60a3f7c6f2384879df4b8ed26efa&amp;scene=21#wechat_redirect" target="_blank" rel="noopener">gzip解码问题</a></p>
<p><img src="https://segmentfault.com/img/bVbfVFU" alt="clipboard.png"><br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="keyword">import</span> gzip</span><br><span class="line"></span><br><span class="line"># 注意要先读取内容才可以解压缩</span><br><span class="line">data = urllib.request.urlopen(url).read()</span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    html =data.decode(<span class="string">"utf-8"</span>)</span><br><span class="line">except:</span><br><span class="line">    html =gzip.decompress(data).decode(<span class="string">"utf-8"</span>)</span><br></pre></td></tr></table></figure></p>
<h3 id="Intervention-Image-图片写入中文"><a href="#Intervention-Image-图片写入中文" class="headerlink" title="Intervention/Image 图片写入中文"></a>Intervention/Image 图片写入中文</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//laravel-china.org/topics/13642/problems-encountered-in-writing-chinese-with-interventionimage-pictures-and-solutions</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">to_unicode</span>(<span class="params">$string</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $str = mb_convert_encoding($string, <span class="string">'UCS-2'</span>, <span class="string">'UTF-8'</span>);</span><br><span class="line">    $arrstr = str_split($str, <span class="number">2</span>);</span><br><span class="line">    $unistr = <span class="string">''</span>;</span><br><span class="line">    foreach ($arrstr <span class="keyword">as</span> $n) &#123;</span><br><span class="line">        $dec = hexdec(bin2hex($n));</span><br><span class="line">        $unistr .= <span class="string">'&amp;#'</span> . $dec . <span class="string">';'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $unistr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="获取某字段修改前的值"><a href="#获取某字段修改前的值" class="headerlink" title="获取某字段修改前的值"></a>获取某字段修改前的值</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Issue::saving(<span class="function"><span class="keyword">function</span>(<span class="params">Issue $issue</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ($issue-&gt;isDirty(<span class="string">'title'</span>)) &#123;</span><br><span class="line">        $user = Auth::user()-&gt;username;</span><br><span class="line">        $oldTitle = $issue-&gt;getOriginal(<span class="string">'title'</span>); <span class="comment">// 原始值</span></span><br><span class="line">        $newTitle = $issue-&gt;title;                <span class="comment">// 新值</span></span><br><span class="line">        ActionLog::log(<span class="string">"$user 把标题 $oldTitle 修改为 $newTitle"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="前一天"><a href="#前一天" class="headerlink" title="前一天"></a>前一天</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//www.cnblogs.com/lhm166/articles/6066762.html</span></span><br><span class="line">$mytime=mktime(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, date(<span class="string">'m'</span>), date(<span class="string">'d'</span>)<span class="number">-1</span>, date(<span class="string">'Y'</span>));<span class="comment">//获取时间戳</span></span><br><span class="line">$mytime=date(<span class="string">"Y-m-d H:i:s"</span>, strtotime(<span class="string">"-1 day"</span>)); <span class="comment">//获取格式为2016-12-30 13:26:13</span></span><br><span class="line">$timetoday = strtotime(date(<span class="string">"Y-m-d"</span>,time()));</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mdate</span>(<span class="params">$time = NULL</span>) </span>&#123;</span><br><span class="line">    $text = <span class="string">''</span>;</span><br><span class="line">    $time = $time === NULL || $time &gt; time() ? time() : intval($time);</span><br><span class="line">    $t = time() - $time; <span class="comment">//时间差 （秒）</span></span><br><span class="line">    $y = date(<span class="string">'Y'</span>, $time)-date(<span class="string">'Y'</span>, time());<span class="comment">//是否跨年</span></span><br><span class="line">    <span class="keyword">switch</span>($t)&#123;</span><br><span class="line">     <span class="keyword">case</span> $t == <span class="number">0</span>:</span><br><span class="line">       $text = <span class="string">'刚刚'</span>;</span><br><span class="line">       <span class="keyword">break</span>;</span><br><span class="line">     <span class="keyword">case</span> $t &lt; <span class="number">60</span>:</span><br><span class="line">      $text = $t . <span class="string">'秒前'</span>; <span class="comment">// 一分钟内</span></span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">     <span class="keyword">case</span> $t &lt; <span class="number">60</span> * <span class="number">60</span>:</span><br><span class="line">      $text = floor($t / <span class="number">60</span>) . <span class="string">'分钟前'</span>; <span class="comment">//一小时内</span></span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">     <span class="keyword">case</span> $t &lt; <span class="number">60</span> * <span class="number">60</span> * <span class="number">24</span>:</span><br><span class="line">      $text = floor($t / (<span class="number">60</span> * <span class="number">60</span>)) . <span class="string">'小时前'</span>; <span class="comment">// 一天内</span></span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">     <span class="keyword">case</span> $t &lt; <span class="number">60</span> * <span class="number">60</span> * <span class="number">24</span> * <span class="number">3</span>:</span><br><span class="line">      $text = floor($time/(<span class="number">60</span>*<span class="number">60</span>*<span class="number">24</span>)) ==<span class="number">1</span> ?<span class="string">'昨天 '</span> . date(<span class="string">'H:i'</span>, $time) : <span class="string">'前天 '</span> . date(<span class="string">'H:i'</span>, $time) ; <span class="comment">//昨天和前天</span></span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">     <span class="keyword">case</span> $t &lt; <span class="number">60</span> * <span class="number">60</span> * <span class="number">24</span> * <span class="number">30</span>:</span><br><span class="line">      $text = date(<span class="string">'m月d日 H:i'</span>, $time); <span class="comment">//一个月内</span></span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">     <span class="keyword">case</span> $t &lt; <span class="number">60</span> * <span class="number">60</span> * <span class="number">24</span> * <span class="number">365</span>&amp;&amp;$y==<span class="number">0</span>:</span><br><span class="line">      $text = date(<span class="string">'m月d日'</span>, $time); <span class="comment">//一年内</span></span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">     <span class="keyword">default</span>:</span><br><span class="line">      $text = date(<span class="string">'Y年m月d日'</span>, $time); <span class="comment">//一年以前</span></span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">        </span><br><span class="line">    <span class="keyword">return</span> $text;</span><br><span class="line">&#125;<span class="function"><span class="keyword">function</span> <span class="title">friend_date</span>(<span class="params">$time</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!$time)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    $fdate = <span class="string">''</span>;</span><br><span class="line">    $d = time() - intval($time);</span><br><span class="line">    $ld = $time - mktime(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, date(<span class="string">'Y'</span>)); <span class="comment">//得出年</span></span><br><span class="line">    $md = $time - mktime(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, date(<span class="string">'m'</span>), <span class="number">0</span>, date(<span class="string">'Y'</span>)); <span class="comment">//得出月</span></span><br><span class="line">    $byd = $time - mktime(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, date(<span class="string">'m'</span>), date(<span class="string">'d'</span>) - <span class="number">2</span>, date(<span class="string">'Y'</span>)); <span class="comment">//前天</span></span><br><span class="line">    $yd = $time - mktime(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, date(<span class="string">'m'</span>), date(<span class="string">'d'</span>) - <span class="number">1</span>, date(<span class="string">'Y'</span>)); <span class="comment">//昨天</span></span><br><span class="line">    $dd = $time - mktime(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, date(<span class="string">'m'</span>), date(<span class="string">'d'</span>), date(<span class="string">'Y'</span>)); <span class="comment">//今天</span></span><br><span class="line">    $td = $time - mktime(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, date(<span class="string">'m'</span>), date(<span class="string">'d'</span>) + <span class="number">1</span>, date(<span class="string">'Y'</span>)); <span class="comment">//明天</span></span><br><span class="line">    $atd = $time - mktime(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, date(<span class="string">'m'</span>), date(<span class="string">'d'</span>) + <span class="number">2</span>, date(<span class="string">'Y'</span>)); <span class="comment">//后天</span></span><br><span class="line">    <span class="keyword">if</span> ($d == <span class="number">0</span>) &#123;</span><br><span class="line">        $fdate = <span class="string">'刚刚'</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">switch</span> ($d) &#123;</span><br><span class="line">            <span class="keyword">case</span> $d &lt; $atd:</span><br><span class="line">                $fdate = date(<span class="string">'Y年m月d日'</span>, $time);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> $d &lt; $td:</span><br><span class="line">                $fdate = <span class="string">'后天'</span> . date(<span class="string">'H:i'</span>, $time);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> $d &lt; <span class="number">0</span>:</span><br><span class="line">                $fdate = <span class="string">'明天'</span> . date(<span class="string">'H:i'</span>, $time);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> $d &lt; <span class="number">60</span>:</span><br><span class="line">                $fdate = $d . <span class="string">'秒前'</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> $d &lt; <span class="number">3600</span>:</span><br><span class="line">                $fdate = floor($d / <span class="number">60</span>) . <span class="string">'分钟前'</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> $d &lt; $dd:</span><br><span class="line">                $fdate = floor($d / <span class="number">3600</span>) . <span class="string">'小时前'</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> $d &lt; $yd:</span><br><span class="line">                $fdate = <span class="string">'昨天'</span> . date(<span class="string">'H:i'</span>, $time);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> $d &lt; $byd:</span><br><span class="line">                $fdate = <span class="string">'前天'</span> . date(<span class="string">'H:i'</span>, $time);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> $d &lt; $md:</span><br><span class="line">                $fdate = date(<span class="string">'m月d日 H:i'</span>, $time);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> $d &lt; $ld:</span><br><span class="line">                $fdate = date(<span class="string">'m月d日'</span>, $time);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">                $fdate = date(<span class="string">'Y年m月d日'</span>, $time);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $fdate;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="phpstorm-xdebug"><a href="#phpstorm-xdebug" class="headerlink" title="phpstorm xdebug"></a>phpstorm xdebug</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//www.cnblogs.com/baocheng/p/5775938.html</span></span><br><span class="line">https:<span class="comment">//www.cnblogs.com/niuxiaoling/p/8027161.html</span></span><br><span class="line">https:<span class="comment">//laravel-china.org/topics/13275/vagrant-phpstorm-xdebug</span></span><br><span class="line">https:<span class="comment">//laravel-china.org/articles/12974/phpstrom-xdebug-breakpoint-debug-tutorial</span></span><br><span class="line">https:<span class="comment">//laravel-china.org/articles/16425</span></span><br><span class="line">如果出现Can<span class="string">'t start listening for connections from '</span>xdebug<span class="string">': Port 9000 is busy</span></span><br><span class="line"><span class="string">可修改端口号，要修改两个地方</span></span><br><span class="line"><span class="string">第一个地方是刚才php.ini里面的xdebug.remote_port=9000</span></span><br><span class="line"><span class="string">第二个地方是phpstorm - setting - Languages&amp;Frameworks - 相应的语言（比如PHP） - debug - 修改里面的Debug port</span></span><br></pre></td></tr></table></figure>
<h3 id="Python-for-循环中的陷阱"><a href="#Python-for-循环中的陷阱" class="headerlink" title="Python for 循环中的陷阱"></a>Python for 循环中的陷阱</h3><p><a href="https://www.v2ex.com/t/470443#reply38" target="_blank" rel="noopener">https://www.v2ex.com/t/470443#reply38</a></p>
<h3 id="pyinstaller-打包-exe"><a href="#pyinstaller-打包-exe" class="headerlink" title="pyinstaller 打包 exe"></a>pyinstaller 打包 exe</h3><p>pyinstaller -p venv_path (中间一定要有空格) you.py <a href="https://www.v2ex.com/t/476247#reply25" target="_blank" rel="noopener">https://www.v2ex.com/t/476247#reply25</a><br><a href="http://ju.outofmemory.cn/entry/137370" target="_blank" rel="noopener">http://ju.outofmemory.cn/entry/137370</a> </p>
<h3 id="php捕获fatal-error"><a href="#php捕获fatal-error" class="headerlink" title="php捕获fatal error"></a>php捕获fatal error</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fatal_handler</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    $errfile = <span class="string">"unknown file"</span>;</span><br><span class="line">    $errstr  = <span class="string">"shutdown"</span>;</span><br><span class="line">    $errno   = E_CORE_ERROR;</span><br><span class="line">    $errline = <span class="number">0</span>;</span><br><span class="line">    $error = error_get_last();</span><br><span class="line">    <span class="keyword">if</span>($error)&#123;</span><br><span class="line">      <span class="comment">//发送邮件队列也可以http://www.iamtb.cn/2017/11/17/php-catch-fatel-error/</span></span><br><span class="line">        file_put_contents(<span class="string">'./testerror11.txt'</span>, json_encode($error));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">register_shutdown_function(<span class="string">"fatal_handler"</span>);</span><br><span class="line"><span class="keyword">try</span>&#123;</span><br><span class="line">    $db=<span class="keyword">new</span> db();</span><br><span class="line">&#125;<span class="keyword">catch</span>(Exception $e)&#123;</span><br><span class="line">echo $e-&gt;error_msg();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="PHP7开始，含十六进制字符串不再被认为是数字"><a href="#PHP7开始，含十六进制字符串不再被认为是数字" class="headerlink" title="PHP7开始，含十六进制字符串不再被认为是数字"></a>PHP7开始，含十六进制字符串不再被认为是数字</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">$str = <span class="string">"0xffff"</span>;<span class="comment">//https://segmentfault.com/q/1010000004829059</span></span><br><span class="line">$int = filter_var($str, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX);</span><br><span class="line"><span class="keyword">if</span> (<span class="literal">false</span> === $int) &#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> Exception(<span class="string">"Invalid integer!"</span>);</span><br><span class="line">&#125;</span><br><span class="line">var_dump($int); <span class="comment">// int(65535)</span></span><br><span class="line">$t1 = <span class="number">0x3FFFFFFF</span> &amp; (<span class="number">1</span> * (<span class="number">0xd5b42e11</span>));</span><br><span class="line">$t2 = <span class="number">0x3FFFFFFF</span> &amp; (<span class="number">1</span> * (filter_var(<span class="string">"0xd5b42e11"</span>, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX)));</span><br><span class="line"></span><br><span class="line">var_dump($t1,$t2);</span><br></pre></td></tr></table></figure>
<h3 id="php-artisan-config-cache"><a href="#php-artisan-config-cache" class="headerlink" title="php artisan config:cache"></a>php artisan config:cache</h3><p><img src="https://segmentfault.com/img/bVbgb3d" alt="clipboard.png"><br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">cat config/sentry.php</span><br><span class="line"><span class="keyword">return</span> array(</span><br><span class="line">    <span class="string">'dsn'</span> =&gt; env(<span class="string">'SENTRY_LARAVEL_DSN'</span>),</span><br><span class="line"></span><br><span class="line">    <span class="comment">// capture release as git sha</span></span><br><span class="line">    <span class="comment">// 'release' =&gt; trim(exec('git log --pretty="%h" -n1 HEAD')),</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// Capture bindings on SQL queries</span></span><br><span class="line">    <span class="string">'breadcrumbs.sql_bindings'</span> =&gt; <span class="literal">true</span>,</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Capture default user context</span></span><br><span class="line">    <span class="string">'user_context'</span> =&gt; <span class="literal">false</span>,</span><br><span class="line"></span><br><span class="line">    <span class="comment">//transport function</span></span><br><span class="line">    <span class="string">'transport'</span>=&gt;<span class="function"><span class="keyword">function</span>(<span class="params">Raven_Client $raven_Client,&amp;$data</span>)</span>&#123;</span><br><span class="line">        $raven_Client-&gt;setTransport(<span class="literal">null</span>);</span><br><span class="line">          $raven_Client-&gt;close_curl_resource();</span><br><span class="line">            \Queue::pushOn(<span class="string">'sentry'</span>,<span class="keyword">new</span> \App\Commands\sentry($raven_Client,$data));</span><br><span class="line">        &#125;,</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="string">'transport'</span>=&gt;<span class="keyword">new</span> \app\SentryTransport(),</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SentryTransport</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">__set_state</span>(<span class="params">$arr</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params">$raven_Client,&amp;$data</span>)</span>&#123;</span><br><span class="line">            \Queue::pushOn(<span class="string">'sentry'</span>,<span class="keyword">new</span> \App\Commands\sentry($raven_Client,$data));</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="curl超时"><a href="#curl超时" class="headerlink" title="curl超时"></a>curl超时</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//重试</span></span><br><span class="line">$ch = curl_init();</span><br><span class="line"><span class="comment">// curl_setopt ($ch, CURLOPT_URL, 'produc_redis.php.com');</span></span><br><span class="line">curl_setopt ($ch, CURLOPT_URL, <span class="string">'11.11.11.1'</span>);</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//segmentfault.com/a/1190000011188541</span></span><br><span class="line"><span class="comment">// I changed UA here</span></span><br><span class="line">curl_setopt ($ch, CURLOPT_USERAGENT, <span class="string">'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1'</span>);</span><br><span class="line"></span><br><span class="line">curl_setopt ($ch, CURLOPT_RETURNTRANSFER, <span class="literal">true</span>);</span><br><span class="line">curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, <span class="number">2</span>);</span><br><span class="line">curl_setopt ($ch, CURLOPT_AUTOREFERER, <span class="literal">true</span>);</span><br><span class="line">curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, <span class="literal">false</span>);</span><br><span class="line">curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, <span class="number">2</span>);</span><br><span class="line"></span><br><span class="line">$html = curl_exec($ch);</span><br><span class="line"></span><br><span class="line">var_dump($html,curl_error($ch));</span><br><span class="line"></span><br><span class="line">$num=<span class="number">3</span>;</span><br><span class="line"><span class="keyword">for</span>($i=<span class="number">1</span>;$i&lt;=$num;$i++)&#123;</span><br><span class="line">    <span class="keyword">if</span>(curl_getinfo($ch,CURLINFO_HTTP_CODE)==<span class="string">'0'</span> &amp;&amp; $i&lt;=$num)&#123;</span><br><span class="line">      echo <span class="string">"retry $i 次"</span>.PHP_EOL;</span><br><span class="line">      <span class="keyword">if</span>($i==<span class="number">3</span>)&#123;</span><br><span class="line">          curl_setopt ($ch, CURLOPT_URL, <span class="string">'220.181.57.217'</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      $html = curl_exec($ch);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">var_dump($html);</span><br><span class="line"></span><br><span class="line">var_dump(curl_error($ch));</span><br><span class="line"></span><br><span class="line"><span class="comment">// var_dump(curl_getinfo($ch));</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * curl请求</span></span><br><span class="line"><span class="comment"> *https://segmentfault.com/a/1190000010197068</span></span><br><span class="line"><span class="comment"> * @param $url</span></span><br><span class="line"><span class="comment"> * @param string $postData</span></span><br><span class="line"><span class="comment"> * @param int $timeout</span></span><br><span class="line"><span class="comment"> * @return array|mixed</span></span><br><span class="line"><span class="comment"> * @throws Exception</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">protected <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">post</span>(<span class="params">$url, $postData = <span class="string">''</span>, $timeout = <span class="number">5</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $ret = array();</span><br><span class="line">    $times = <span class="number">5</span>;</span><br><span class="line">    <span class="keyword">do</span> &#123;</span><br><span class="line">        $ch = curl_init();</span><br><span class="line">        curl_setopt($ch, CURLOPT_URL, $url);</span><br><span class="line">        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, <span class="string">"POST"</span>);</span><br><span class="line">        curl_setopt($ch, CURLOPT_POST, <span class="literal">true</span>);</span><br><span class="line">        curl_setopt($ch, CURLOPT_HEADER, <span class="literal">false</span>);</span><br><span class="line">        <span class="keyword">if</span> ($postData != <span class="string">''</span>) &#123;</span><br><span class="line">            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);</span><br><span class="line">        &#125;</span><br><span class="line">        curl_setopt($ch, CURLOPT_RETURNTRANSFER, <span class="literal">true</span>);</span><br><span class="line">        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);</span><br><span class="line">        <span class="comment">// 重要, 该处不能丢 curl 执行最大秒数</span></span><br><span class="line">        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);</span><br><span class="line">        $output = curl_exec($ch);</span><br><span class="line">        <span class="keyword">if</span> ($errNo = curl_errno($ch)) &#123;</span><br><span class="line">            error_log(<span class="string">"Error [$errNo]: "</span> . curl_error($ch));</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $ret = json_decode($output, <span class="literal">true</span>);</span><br><span class="line">            <span class="comment">// 解析的结果集为空时停止查询</span></span><br><span class="line">            <span class="keyword">if</span> (!is_array($ret) &amp;&amp; !trim($ret)) &#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> Exception(__METHOD__ . <span class="string">": cURL调用失败, 信息为: "</span> . $output);</span><br><span class="line">            &#125;</span><br><span class="line">            unset($output);</span><br><span class="line">        &#125;</span><br><span class="line">        curl_close($ch);</span><br><span class="line">        <span class="keyword">if</span> (isset($ret[<span class="number">0</span>]) &amp;&amp; $ret[<span class="number">0</span>]) &#123;</span><br><span class="line">            <span class="keyword">return</span> $ret;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">while</span> ($times--);</span><br><span class="line">    exit(__METHOD__ . <span class="string">": cURL请求重试至 &#123;$times&#125; 次后仍无响应, 执行退出"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="cURL-error-28"><a href="#cURL-error-28" class="headerlink" title="cURL error 28"></a>cURL error 28</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">cURL error <span class="number">28</span>: See http:<span class="comment">//curl.haxx.se/libcurl/c/libcurl-errors.html</span></span><br><span class="line">$client=<span class="keyword">new</span> GuzzleHttp\Client();</span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    $tmp = $client-&gt;get($url,array(<span class="string">"timeout"</span>=&gt;<span class="number">3</span>,<span class="string">"connect_timeout"</span>=&gt;<span class="number">3</span>));</span><br><span class="line">    $res = json_decode(strval($tmp-&gt;getBody()),<span class="number">1</span>);</span><br><span class="line">&#125; <span class="keyword">catch</span> (\Exception $e) &#123;</span><br><span class="line">    $res = [];</span><br><span class="line">    \Log::info(<span class="string">'error'</span>, [<span class="string">'url'</span> =&gt; $url, <span class="string">'msg'</span> =&gt; $e-&gt;getMessage()]);</span><br><span class="line">&#125;</span><br><span class="line">$client=<span class="keyword">new</span> GuzzleHttp\Client();</span><br><span class="line">		$requests=[</span><br><span class="line">			$client-&gt;createRequest(<span class="string">'GET'</span>,$url,[<span class="string">"timeout"</span>=&gt;<span class="number">3</span>,<span class="string">"connect_timeout"</span>=&gt;<span class="number">3</span>]),</span><br><span class="line">			$client-&gt;createRequest(<span class="string">'GET'</span>,$url2,[<span class="string">"timeout"</span>=&gt;<span class="number">3</span>,<span class="string">"connect_timeout"</span>=&gt;<span class="number">3</span>]),</span><br><span class="line">			</span><br><span class="line">		];</span><br><span class="line">$result = Pool::batch($client, $requests, [<span class="string">'pool_size'</span> =&gt; <span class="number">2</span>]);</span><br><span class="line"><span class="comment">//$result-&gt;getResult($requests[0]) instanceof GuzzleHttp\Message\Response</span></span><br><span class="line"><span class="keyword">if</span> ($result[<span class="number">0</span>] <span class="keyword">instanceof</span> Response &amp;&amp; $result[<span class="number">0</span>]-&gt;getStatusCode() == <span class="number">200</span>) &#123;</span><br><span class="line">            $data = json_decode($result[<span class="number">0</span>]-&gt;getBody(), <span class="literal">true</span>);</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h3 id="格式化"><a href="#格式化" class="headerlink" title="格式化"></a>格式化</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> prettytable <span class="keyword">as</span> pt</span><br><span class="line">https:<span class="comment">//www.v2ex.com/t/483984#reply15</span></span><br><span class="line">## 按行添加数据</span><br><span class="line">tb = pt.PrettyTable()</span><br><span class="line">tb.field_names = [<span class="string">"City name"</span>, <span class="string">"Area"</span>, <span class="string">"Population"</span>, <span class="string">"Annual Rainfall"</span>]</span><br><span class="line">tb.add_row([<span class="string">"Adelaide"</span>,<span class="number">1295</span>, <span class="number">1158259</span>, <span class="number">600.5</span>])</span><br><span class="line">tb.add_row([<span class="string">"Brisbane"</span>,<span class="number">5905</span>, <span class="number">1857594</span>, <span class="number">1146.4</span>])</span><br><span class="line">tb.add_row([<span class="string">"Darwin"</span>, <span class="number">112</span>, <span class="number">120900</span>, <span class="number">1714.7</span>])</span><br><span class="line">tb.add_row([<span class="string">"Hobart"</span>, <span class="number">1357</span>, <span class="number">205556</span>,<span class="number">619.5</span>])</span><br><span class="line"></span><br><span class="line">print(tb)</span><br><span class="line"> #str.ljust 和 rjust 才是王道，谁用谁知道！</span><br><span class="line"></span><br><span class="line">+-----------+------+------------+-----------------+</span><br><span class="line">| City name | Area | Population | Annual Rainfall |</span><br><span class="line">+-----------+------+------------+-----------------+</span><br><span class="line">| Adelaide | <span class="number">1295</span> | <span class="number">1158259</span> | <span class="number">600.5</span> |</span><br><span class="line">| Brisbane | <span class="number">5905</span> | <span class="number">1857594</span> | <span class="number">1146.4</span> |</span><br><span class="line">| Darwin | <span class="number">112</span> | <span class="number">120900</span> | <span class="number">1714.7</span> |</span><br><span class="line">| Hobart | <span class="number">1357</span> | <span class="number">205556</span> | <span class="number">619.5</span> |</span><br><span class="line">+-----------+------+------------+-----------------+</span><br></pre></td></tr></table></figure>
<h3 id="unicode"><a href="#unicode" class="headerlink" title="unicode"></a>unicode</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">Unicode 每个字符的详情，可以查官方文档： https:<span class="comment">//www.unicode.org/charts/</span></span><br><span class="line">python2 代码：</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">0x005b</span>,<span class="number">0x005f</span>):</span><br><span class="line">print (unichr(i))#print (chr(i))</span><br><span class="line">python3 代码请查看 http:<span class="comment">//www.chenxm.cc/post/683.html</span></span><br><span class="line">unicode 字符表示方法 \u + <span class="number">16</span> 进制数值,例如 \u0001 print(<span class="string">''</span>.join(chr(u) <span class="keyword">for</span> u <span class="keyword">in</span> range(<span class="number">0</span>, <span class="number">0x41</span>))) \u0040 这里 <span class="number">0040</span> 就是 <span class="number">16</span> 进制的，range 是左闭右开的，所以\u0020-\u0040 的码点范围是 range(<span class="number">0x00</span>, <span class="number">0x40</span>+<span class="number">1</span>)</span><br><span class="line"><span class="number">0</span>b 二进制 <span class="number">0</span>o 八进制 <span class="number">0</span>x <span class="number">16</span> 进制</span><br><span class="line"><span class="number">0b10</span> <span class="comment">// 2 </span></span><br><span class="line"><span class="number">0o10</span> <span class="comment">// 8</span></span><br><span class="line"><span class="number">0x10</span> <span class="comment">// 16</span></span><br><span class="line">unicode 字符表示方法 \u + <span class="number">16</span> 进制数值,例如 \u0001</span><br><span class="line">print(u<span class="string">'\u0061'</span>) <span class="comment">// ahttps://www.v2ex.com/t/484923#reply15</span></span><br></pre></td></tr></table></figure>
<h3 id="laravel更新后读取数据失败"><a href="#laravel更新后读取数据失败" class="headerlink" title="laravel更新后读取数据失败"></a>laravel更新后读取数据失败</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//laravel-china.org/articles/16566/take-a-laravel-mysql-to-verify-the-experience-of-bug#reply62596</span></span><br><span class="line"><span class="comment">//创建数据</span></span><br><span class="line">$meeting = meeting::create($data);</span><br><span class="line"></span><br><span class="line"><span class="comment">//查询数据</span></span><br><span class="line">$meeting = meeting::findOrFail($meeting-&gt;id);</span><br><span class="line"></span><br><span class="line"><span class="comment">//纪录操作日志</span></span><br><span class="line">Log::insert($meeting);</span><br><span class="line">线上数据库是读写分离的，主库写入，但更新数据后马上查询，从库可能没来得及更新数据</span><br><span class="line">当网络不好时遇到从库更新不及时 findOrFail 就会报 <span class="number">404</span>，导致后面所有操作都失效，自然验证也失效了</span><br><span class="line">解决方法</span><br><span class="line"></span><br><span class="line">更新完后，查询指定使用主库</span><br><span class="line">更新后禁止查询，直接使用 create 返回的 model 实例，对于数据库中的有默认值的字段，手动加入到 model 实例中</span><br></pre></td></tr></table></figure>
<h3 id="批量更新"><a href="#批量更新" class="headerlink" title="批量更新"></a>批量更新</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://laravel-china.org/topics/6864/how-to-batch-update-accumulate-a-field</span></span><br><span class="line">https:<span class="comment">//laravel-china.org/articles/16620/laravel-project-execl-batch-import-database-data-update-data-no-insertion</span></span><br><span class="line">$arr = [</span><br><span class="line"><span class="comment">// user_id =&gt; reward</span></span><br><span class="line">  <span class="string">'1'</span> =&gt; <span class="number">50</span>,</span><br><span class="line">  <span class="string">'2'</span> =&gt; <span class="number">30</span>,</span><br><span class="line">  <span class="string">'3'</span> =&gt; <span class="number">60</span></span><br><span class="line">];</span><br><span class="line">User::find(array_keys($arr))-&gt;each(<span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) <span class="title">use</span> (<span class="params">$arr</span>) </span>&#123;</span><br><span class="line">  $query-&gt;increment(<span class="string">'award'</span> , $arr[$query-&gt;id]);</span><br><span class="line">&#125;);</span><br><span class="line"># 批量更新相同的 award</span><br><span class="line">User::find(array_keys($arr))-&gt;each-&gt;increment(<span class="string">'award'</span>, <span class="number">30</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">         * 批量更新表的值，防止阻塞</span></span><br><span class="line"><span class="comment">         * @note 生成的SQL语句如下：</span></span><br><span class="line"><span class="comment">         * update mj_node set sort = case id</span></span><br><span class="line"><span class="comment">         *      when 13 then 1</span></span><br><span class="line"><span class="comment">         *      when 1 then 4</span></span><br><span class="line"><span class="comment">         *      when 7 then 5</span></span><br><span class="line"><span class="comment">         *      when 8 then 6</span></span><br><span class="line"><span class="comment">         *      when 9 then 7</span></span><br><span class="line"><span class="comment">         *      when 10 then 8</span></span><br><span class="line"><span class="comment">         *      when 11 then 9</span></span><br><span class="line"><span class="comment">         *      when 12 then 10</span></span><br><span class="line"><span class="comment">         * end where id in (13,1,7,8,9,10,11,12)</span></span><br><span class="line"><span class="comment">         * @param $conditions_field 条件字段</span></span><br><span class="line"><span class="comment">         * @param $values_field  需要被更新的字段</span></span><br><span class="line"><span class="comment">         * @param $conditions</span></span><br><span class="line"><span class="comment">         * @param $values</span></span><br><span class="line"><span class="comment">         * @return int</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">batchUpdate</span>(<span class="params">$conditions_field, $values_field, $conditions, $values</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">                $table = $<span class="keyword">this</span>-&gt;model-&gt;getFullTableName();<span class="comment">//返回完整表名，自己在项目中定义</span></span><br><span class="line">                $sql   = <span class="string">'update '</span> . $table . <span class="string">' set '</span>. $values_field .<span class="string">' = case '</span> .$conditions_field;</span><br><span class="line">                foreach ($conditions <span class="keyword">as</span> $key =&gt; $condition) &#123;</span><br><span class="line">                        $sql .= <span class="string">' when '</span> . $condition . <span class="string">' then ?'</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                $sql .= <span class="string">' end where id in ('</span> . implode(<span class="string">','</span>, $conditions) . <span class="string">')'</span>;</span><br><span class="line">                <span class="keyword">return</span> DB::update($sql, $values);<span class="comment">//项目中需要引入DB  facade</span></span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
<h3 id="cat-多换行"><a href="#cat-多换行" class="headerlink" title="cat 多换行"></a>cat 多换行</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//www.v2ex.com/t/188162</span></span><br><span class="line">调试shell脚本可以使用 sh -x ****.sh，有详细输出内容</span><br><span class="line">Bash的echo默认在末尾加了换行符<span class="string">"\n"</span>，所以你cat那个文件多了一个换行符，给echo加上-n参数就能不自动加换行符了。</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span>= cat tmp.txt;echo $<span class="keyword">var</span><span class="string">"abc"</span> 改成如下的形式</span><br><span class="line"></span><br><span class="line">var= `cat tmp.txt`;echo $var"abc"或者 #注意不是单引号，是tab键上面1前面的那个符号</span><br><span class="line"><span class="keyword">var</span>=$(cat tmp.txt);echo $<span class="keyword">var</span><span class="string">"abc"</span></span><br><span class="line">grep显示前后几行信息</span><br><span class="line">grep -C <span class="number">5</span> foo file 显示file文件里匹配foo字串那行以及上下<span class="number">5</span>行</span><br><span class="line">grep -B <span class="number">5</span> foo file 显示foo及前<span class="number">5</span>行</span><br><span class="line">grep -A <span class="number">5</span> foo file 显示foo及后<span class="number">5</span>行</span><br><span class="line">$grep <span class="number">-5</span> <span class="string">'parttern'</span> inputfile <span class="comment">//打印匹配行的前后5行</span></span><br><span class="line">查看某一个文件第<span class="number">5</span>行和第<span class="number">10</span>行</span><br><span class="line"></span><br><span class="line"> sed -n <span class="string">'5,10p'</span> filename 这样你就可以只查看文件的第<span class="number">5</span>行到第<span class="number">10</span>行。</span><br></pre></td></tr></table></figure>
<h3 id="grep-and-or"><a href="#grep-and-or" class="headerlink" title="grep and or"></a>grep and or</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//blog.csdn.net/jackaduma/article/details/6900242</span></span><br><span class="line">grep -E <span class="string">'Tech|Sales'</span> employee.txt</span><br><span class="line">grep -v <span class="string">'pattern1'</span> filename</span><br><span class="line">$ grep -E 'Manager.*Sales|Sales.*Manager' employee.txt  # -E 'pattern1.*pattern2'</span><br><span class="line">egrep <span class="string">"php.*artisan.*$1"</span></span><br><span class="line">egrep <span class="string">'pattern1|pattern2'</span> filename</span><br></pre></td></tr></table></figure>
<h3 id="memcached-默认的过期时间则-不能大于-30-天"><a href="#memcached-默认的过期时间则-不能大于-30-天" class="headerlink" title="memcached 默认的过期时间则 不能大于 30 天"></a>memcached 默认的过期时间则 不能大于 30 天</h3><p><a href="https://laravel-china.org/topics/2461/laravel-session-using-memcached-stepping-on-the-pit" target="_blank" rel="noopener">https://laravel-china.org/topics/2461/laravel-session-using-memcached-stepping-on-the-pit</a></p>
<p><img src="https://segmentfault.com/img/bVbgxJQ?w=782&amp;h=387" alt="clipboard.png"></p>
<h3 id="unix-tmp-supervisor-sock-refused-connection"><a href="#unix-tmp-supervisor-sock-refused-connection" class="headerlink" title="unix:///tmp/supervisor.sock refused connection"></a>unix:///tmp/supervisor.sock refused connection</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost supervisor]# supervisorctl -c /etc/supervisord.conf</span><br><span class="line">unix:<span class="comment">///tmp/supervisor.sock refused connection</span></span><br><span class="line">supervisor&gt; exit</span><br><span class="line"> rm -f /tmp/supervisor.sock</span><br><span class="line">[root@localhost supervisor]# ps aux|grep super</span><br><span class="line">root        <span class="number">48</span>  <span class="number">0.0</span>  <span class="number">0.0</span>      <span class="number">0</span>     <span class="number">0</span> ?        S     <span class="number">2017</span>   <span class="number">2</span>:<span class="number">17</span> [sync_supers]</span><br><span class="line">root      <span class="number">8246</span>  <span class="number">0.0</span>  <span class="number">0.1</span> <span class="number">208064</span> <span class="number">13404</span> ?        Ss   <span class="number">18</span>:<span class="number">17</span>   <span class="number">0</span>:<span class="number">00</span> /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf</span><br><span class="line">supervisord是服务端，是个deamon，supervisorctl是客户https:<span class="comment">//www.v2ex.com/t/210122</span></span><br><span class="line">修改supervisor.conf文件后，要执行supervisorctl reload，重新加载配置文件</span><br><span class="line">vi artisan.conf</span><br><span class="line">[root@localhost supervisor]# supervisorctl status</span><br><span class="line">unix:<span class="comment">///tmp/supervisor.sock no such file</span></span><br><span class="line">[root@localhost supervisor]# ps aux|grep super</span><br><span class="line">root        <span class="number">48</span>  <span class="number">0.0</span>  <span class="number">0.0</span>      <span class="number">0</span>     <span class="number">0</span> ?        S     <span class="number">2017</span>   <span class="number">2</span>:<span class="number">17</span> [sync_supers]</span><br><span class="line">root     <span class="number">19472</span>  <span class="number">0.0</span>  <span class="number">0.0</span> <span class="number">103256</span>   <span class="number">844</span> pts/<span class="number">5</span>    S+   <span class="number">19</span>:<span class="number">57</span>   <span class="number">0</span>:<span class="number">00</span> grep <span class="keyword">super</span></span><br><span class="line">[root@localhost supervisor]# supervisord -c /etc/supervisord.conf</span><br><span class="line"><span class="built_in">Error</span>: Invalid user name www <span class="keyword">in</span> section <span class="string">'program:artisan_queue'</span> (file: <span class="string">'/etc/supervisor/artisan.conf'</span>)</span><br><span class="line">For help, use /usr/bin/supervisord -h</span><br><span class="line">[root@localhost supervisor]# vi /etc/supervisor/artisan.conf</span><br><span class="line">[root@localhost supervisor]# supervisord -c /etc/supervisord.conf</span><br><span class="line">[root@localhost supervisor]# supervisorctl status</span><br><span class="line">artisan_queue                    BACKOFF   Exited too quickly (process log may have details)</span><br><span class="line"></span><br><span class="line">http:<span class="comment">//blog.51cto.com/jxlwc/2112062 </span></span><br><span class="line">https:<span class="comment">//www.jianshu.com/p/3658c963d28b</span></span><br></pre></td></tr></table></figure>
<h3 id="Specified-key-was-too-long"><a href="#Specified-key-was-too-long" class="headerlink" title="Specified key was too long"></a>Specified key was too long</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//laravel-china.org/articles/17094/larave-admin-installation?#reply64528</span></span><br><span class="line">默认的数据库字符集，现在utf8mb4包括存储emojis支持。如果你运行MySQL v5<span class="number">.7</span><span class="number">.7</span>或者更高版本，则不需要做任何事情。</span><br><span class="line">https:<span class="comment">//laravel-china.org/articles/4195/laravel-54-common-error-specified-key-was-too-long</span></span><br><span class="line">https:<span class="comment">//segmentfault.com/a/1190000008416200</span></span><br><span class="line">把config下的database.php下的mysql设置中,将<span class="string">'strict'</span> =&gt; <span class="literal">false</span>就行了..</span><br><span class="line">Schema::defaultStringLength(<span class="number">191</span>); 是设置好的默认的字符串长度，也就是说，迁移中的字段没有设置长度的话，varchar 字段会被默认成长度只有 <span class="number">191</span></span><br><span class="line">use Illuminate\Support\Facades\Schema;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Schema::defaultStringLength(<span class="number">191</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="laravel-with-查询中只查询个别字段"><a href="#laravel-with-查询中只查询个别字段" class="headerlink" title="laravel with 查询中只查询个别字段"></a>laravel with 查询中只查询个别字段</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">select 的时候没有 id 的话就会为 <span class="literal">null</span> https:<span class="comment">//laravel-china.org/articles/17379</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">brand</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsTo(Brand::<span class="class"><span class="keyword">class</span>, '<span class="title">brandId</span>')-&gt;<span class="title">select</span>('<span class="title">id</span>', '<span class="title">briefCode</span>')</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> $result = Brand::<span class="keyword">with</span>([<span class="string">'brand'</span> =&gt;  <span class="function"><span class="keyword">function</span>(<span class="params">$query</span>)</span>&#123;</span><br><span class="line">    $query-&gt;select(<span class="string">'id'</span>, <span class="string">'briefCode'</span>);</span><br><span class="line">&#125;])-&gt;get();</span><br></pre></td></tr></table></figure>
<h3 id="PHP输出A到Z"><a href="#PHP输出A到Z" class="headerlink" title="PHP输出A到Z"></a>PHP输出A到Z</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>($i=<span class="string">'A'</span>; $i&lt;=<span class="string">'Z'</span>; $i++)</span><br><span class="line">&#123;</span><br><span class="line">    echo $i . <span class="string">'&lt;br&gt;'</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">for</span>($i=ord(<span class="string">'A'</span>); $i&lt;=ord(<span class="string">'Z'</span>); $i++)</span><br><span class="line">&#123;</span><br><span class="line">    echo chr($i) . <span class="string">'&lt;br&gt;'</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">for</span>($i = <span class="string">'A'</span>; $i != <span class="string">'BB'</span>; $i++)</span><br><span class="line">&#123;</span><br><span class="line">    echo $i . <span class="string">'&lt;br&gt;'</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$al = <span class="string">'A'</span>;</span><br><span class="line"><span class="keyword">while</span>($al != <span class="string">'BB'</span>)</span><br><span class="line">&#123;</span><br><span class="line">    echo $al++ . <span class="string">'&lt;br&gt;'</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="env函数的小坑"><a href="#env函数的小坑" class="headerlink" title="env函数的小坑"></a>env函数的小坑</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">dump(env(<span class="string">'APP_ENV'</span>) === <span class="literal">null</span>); <span class="comment">// true </span></span><br><span class="line">因为没有执行 config:cache 命令，而项目上线后为了优化访问速度，生成了缓存文件，导致env取值失败</span><br><span class="line">如果使用了config:cache，env函数只能在config目录下的配置文件的php里使用，不可以在其他地方使用。</span><br><span class="line"></span><br><span class="line">一个非常简单的办法就是将</span><br><span class="line">env(<span class="string">'APP_ENV'</span>)改为config(<span class="string">'app.env'</span>)</span><br><span class="line">config函数会优先读取 bootstrap/cache/config.php 中缓存的配置，如果没有缓存文件，则会直接读取 config 目录下的所有配置文件</span><br><span class="line">https:<span class="comment">//www.jianshu.com/p/83f9cd407751</span></span><br></pre></td></tr></table></figure>
<h3 id="任务调度的列表"><a href="#任务调度的列表" class="headerlink" title="任务调度的列表"></a>任务调度的列表</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1. 加载Console内核</span></span><br><span class="line">app()-&gt;make(\Illuminate\Contracts\Console\Kernel::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//2.  获取计划任务列表</span></span><br><span class="line">$scheduleList = app()-&gt;make(\Illuminate\Console\Scheduling\Schedule::<span class="class"><span class="keyword">class</span>)-&gt;<span class="title">events</span>()</span>;</span><br></pre></td></tr></table></figure>
<h3 id="在模型事件中获取某字段修改前的值"><a href="#在模型事件中获取某字段修改前的值" class="headerlink" title="在模型事件中获取某字段修改前的值"></a>在模型事件中获取某字段修改前的值</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Issue::saving(<span class="function"><span class="keyword">function</span>(<span class="params">Issue $issue</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ($issue-&gt;isDirty(<span class="string">'title'</span>)) &#123;</span><br><span class="line">        $user = Auth::user()-&gt;username;</span><br><span class="line">        $oldTitle = $issue-&gt;getOriginal(<span class="string">'title'</span>); <span class="comment">// 原始值https://laravel-china.org/wikis/16169</span></span><br><span class="line">        $newTitle = $issue-&gt;title;                <span class="comment">// 新值</span></span><br><span class="line">        ActionLog::log(<span class="string">"$user 把标题 $oldTitle 修改为 $newTitle"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="laravel-SerializesModels"><a href="#laravel-SerializesModels" class="headerlink" title="laravel SerializesModels"></a>laravel SerializesModels</h3><p>因为我们在任务类里引用了 SerializesModels 这个 trait，使得 Eloquent 模型在处理任务时可以被优雅地序列化和反序列化。如果你的队列任务类在构造器中接收了一个 Eloquent 模型，那么只有可识别出该模型的属性会被序列化到队列里。当任务被实际运行时，队列系统便会自动从数据库中重新取回完整的模型。这整个过程对你的应用程序来说是完全透明的，这样可以避免在序列化完整的 Eloquent 模式实例时所带来的一些问题。<a href="https://laravel-china.org/topics/2993/laravel-novice-stepped-on-the-pit-to-share" target="_blank" rel="noopener">https://laravel-china.org/topics/2993/laravel-novice-stepped-on-the-pit-to-share</a> </p>
<h3 id="composer-require失败"><a href="#composer-require失败" class="headerlink" title="composer require失败"></a>composer require失败</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br></pre></td><td class="code"><pre><span class="line">cat composer.json</span><br><span class="line"><span class="string">"monolog/monolog"</span>: <span class="string">"^2.0@dev"</span>,</span><br><span class="line"></span><br><span class="line">$ composer <span class="built_in">require</span> dees040/laravel-api-responses</span><br><span class="line">    <span class="number">1</span>/<span class="number">1</span>:        https:<span class="comment">//packagist.laravel-china.org/p/provider-latest$0449728151603                       e355a0ee137fb34881e699652e41e3e1de11ba9c4ea7a47a312.json</span></span><br><span class="line">    Finished: success: <span class="number">1</span>, <span class="attr">skipped</span>: <span class="number">0</span>, <span class="attr">failure</span>: <span class="number">0</span>, <span class="attr">total</span>: <span class="number">1</span></span><br><span class="line">Using version dev-master <span class="keyword">for</span> dees040/laravel-api-responses</span><br><span class="line">./composer.json has been updated</span><br><span class="line">Loading composer repositories <span class="keyword">with</span> package information</span><br><span class="line">Updating dependencies (including <span class="built_in">require</span>-dev)</span><br><span class="line">Your requirements could not be resolved to an installable set <span class="keyword">of</span> packages.</span><br><span class="line"></span><br><span class="line">  Problem <span class="number">1</span></span><br><span class="line">    - The requested package monolog/monolog (locked at <span class="number">1.23</span><span class="number">.0</span>, required <span class="keyword">as</span> ^<span class="number">2.0</span>@dev                       ) is satisfiable by monolog/monolog[<span class="number">1.23</span><span class="number">.0</span>] but these conflict <span class="keyword">with</span> your requiremen                       ts or minimum-stability.</span><br><span class="line">  Problem <span class="number">2</span></span><br><span class="line">    - laravel/framework v5<span class="number">.5</span><span class="number">.42</span> requires monolog/monolog ~<span class="number">1.12</span> -&gt; satisfiable by mo                       nolog/monolog[<span class="number">1.</span>x-dev].</span><br><span class="line">    - laravel/framework v5<span class="number">.5</span><span class="number">.42</span> requires monolog/monolog ~<span class="number">1.12</span> -&gt; satisfiable by mo                       nolog/monolog[<span class="number">1.</span>x-dev].</span><br><span class="line">    - laravel/framework v5<span class="number">.5</span><span class="number">.42</span> requires monolog/monolog ~<span class="number">1.12</span> -&gt; satisfiable by mo                       nolog/monolog[<span class="number">1.</span>x-dev].</span><br><span class="line">    - Can only install one <span class="keyword">of</span>: monolog/monolog[<span class="number">2.</span>x-dev, <span class="number">1.</span>x-dev].</span><br><span class="line">    - Installation request <span class="keyword">for</span> monolog/monolog ^<span class="number">2.0</span>@dev -&gt; satisfiable by monolog/m                       onolog[<span class="number">2.</span>x-dev].</span><br><span class="line">    - Installation request <span class="keyword">for</span> laravel/framework (locked at v5<span class="number">.5</span><span class="number">.42</span>, required <span class="keyword">as</span> <span class="number">5.</span>                       <span class="number">5.</span>*) -&gt; satisfiable by laravel/framework[v5<span class="number">.5</span><span class="number">.42</span>].</span><br><span class="line">    </span><br><span class="line">$composer <span class="built_in">require</span> <span class="string">"monolog/monolog:~1.12"</span></span><br><span class="line">    <span class="number">1</span>/<span class="number">1</span>:        https:<span class="comment">//packagist.laravel-china.org/p/provider-latest$d12a81f48a3e1dad512c1d2c375298e3a8ea414dff5000d5f345939429c6b29b.json</span></span><br><span class="line">    Finished: success: <span class="number">1</span>, <span class="attr">skipped</span>: <span class="number">0</span>, <span class="attr">failure</span>: <span class="number">0</span>, <span class="attr">total</span>: <span class="number">1</span></span><br><span class="line">./composer.json has been updated</span><br><span class="line">Loading composer repositories <span class="keyword">with</span> package information</span><br><span class="line">Updating dependencies (including <span class="built_in">require</span>-dev)</span><br><span class="line">    <span class="number">1</span>/<span class="number">1</span>:        https:<span class="comment">//dl.laravel-china.org/monolog/monolog/c465e1144536862e03f519cb3d65e924062cabfb.zip</span></span><br><span class="line">    Finished: success: <span class="number">1</span>, <span class="attr">skipped</span>: <span class="number">0</span>, <span class="attr">failure</span>: <span class="number">0</span>, <span class="attr">total</span>: <span class="number">1</span></span><br><span class="line">Package operations: <span class="number">0</span> installs, <span class="number">1</span> update, <span class="number">0</span> removals</span><br><span class="line">  - Removing monolog/monolog (<span class="number">1.23</span><span class="number">.0</span>)</span><br><span class="line">  - Installing monolog/monolog (<span class="number">1.</span>x-dev c465e11):</span><br><span class="line">Cloning c465e11445 <span class="keyword">from</span> cache</span><br><span class="line">Package phpoffice/phpexcel is abandoned, you should avoid using it. Use phpoffice/phpspreadsheet instead.</span><br><span class="line">Writing lock file</span><br><span class="line">Generating optimized autoload files</span><br><span class="line">&gt; Illuminate\Foundation\ComposerScripts::postAutoloadDump</span><br><span class="line">&gt; @php artisan package:discover</span><br><span class="line">Discovered Package: <span class="number">96</span>qbhy/hyid</span><br><span class="line">Discovered Package: appstract/laravel-opcache</span><br><span class="line">Discovered Package: appstract/lush-http</span><br><span class="line">Discovered Package: barryvdh/laravel-debugbar</span><br><span class="line">Discovered Package: barryvdh/laravel-dompdf</span><br><span class="line">Discovered Package: barryvdh/laravel-ide-helper</span><br><span class="line">Discovered Package: base62/base62</span><br><span class="line">Discovered Package: cblink/laravel-excel-zip</span><br><span class="line">Discovered Package: cblink/laravel-sso</span><br><span class="line">Discovered Package: chumper/zipper</span><br><span class="line">Discovered Package: encore/laravel-admin</span><br><span class="line">Discovered Package: encore/redis-manager</span><br><span class="line">Discovered Package: fideloper/proxy</span><br><span class="line">Discovered Package: foryoufeng/laravel-generator</span><br><span class="line">Discovered Package: ibrand/laravel-miniprogram-poster</span><br><span class="line">Discovered Package: intervention/image</span><br><span class="line">Discovered Package: itsgoingd/clockwork</span><br><span class="line">Discovered Package: jacobcyl/ali-oss-storage</span><br><span class="line">Discovered Package: jellybool/flysystem-upyun</span><br><span class="line">Discovered Package: jenssegers/agent</span><br><span class="line">Discovered Package: jenssegers/mongodb</span><br><span class="line">Discovered Package: laravel-admin-ext/helpers</span><br><span class="line">Discovered Package: laravel/tinker</span><br><span class="line">Discovered Package: latrell/lock</span><br><span class="line">Discovered Package: lubusin/laravel-decomposer</span><br><span class="line">Discovered Package: maatwebsite/excel</span><br><span class="line">Discovered Package: nesbot/carbon</span><br><span class="line">Discovered Package: overtrue/laravel-query-logger</span><br><span class="line">Discovered Package: overtrue/laravel-socialite</span><br><span class="line">Discovered Package: prettus/l5-repository</span><br><span class="line">Discovered Package: sentry/sentry-laravel</span><br><span class="line">Discovered Package: simplesoftwareio/simple-qrcode</span><br><span class="line">Discovered Package: spatie/laravel-tail</span><br><span class="line">Discovered Package: tymon/jwt-auth</span><br><span class="line">Discovered Package: zgldh/qiniu-laravel-storage</span><br><span class="line">Package manifest generated successfully.</span><br><span class="line"></span><br><span class="line">$ composer <span class="built_in">require</span> dees040/laravel-api-responses</span><br><span class="line">    <span class="number">1</span>/<span class="number">1</span>:        https:<span class="comment">//packagist.laravel-china.org/p/provider-latest$14d9ca1b35af157c565d6c564cde71c59043d7457135c5a4317471625a1db2e7.json</span></span><br><span class="line">    Finished: success: <span class="number">1</span>, <span class="attr">skipped</span>: <span class="number">0</span>, <span class="attr">failure</span>: <span class="number">0</span>, <span class="attr">total</span>: <span class="number">1</span></span><br><span class="line">Using version dev-master <span class="keyword">for</span> dees040/laravel-api-responses</span><br><span class="line">./composer.json has been updated</span><br><span class="line">Loading composer repositories <span class="keyword">with</span> package information</span><br><span class="line">Updating dependencies (including <span class="built_in">require</span>-dev)</span><br><span class="line">    <span class="number">1</span>/<span class="number">1</span>:        https:<span class="comment">//dl.laravel-china.org/dees040/laravel-api-responses/9aec7eb323fc45e17f1dbb9c6e203d240836f91b.zip</span></span><br><span class="line">    Finished: success: <span class="number">1</span>, <span class="attr">skipped</span>: <span class="number">0</span>, <span class="attr">failure</span>: <span class="number">0</span>, <span class="attr">total</span>: <span class="number">1</span></span><br><span class="line">Package operations: <span class="number">1</span> install, <span class="number">0</span> updates, <span class="number">0</span> removals</span><br><span class="line">  - Installing dees040/laravel-api-responses (dev-master <span class="number">9</span>aec7eb): Cloning <span class="number">9</span>aec7eb323 <span class="keyword">from</span> cache</span><br><span class="line">Package phpoffice/phpexcel is abandoned, you should avoid using it. Use phpoffice/phpspreadsheet instead.</span><br><span class="line">Writing lock file</span><br><span class="line">Generating optimized autoload files</span><br><span class="line">&gt; Illuminate\Foundation\ComposerScripts::postAutoloadDump</span><br><span class="line">&gt; @php artisan package:discover</span><br><span class="line"></span><br><span class="line">$ composer <span class="built_in">require</span> phpoffice/phpspreadsheet</span><br><span class="line">    <span class="number">1</span>/<span class="number">2</span>:        https:<span class="comment">//packagist.laravel-china.org/p/provider-latest$8512051929ea08e8189e440a1f5293135717f61525e93d6f4577794143032dd5.json</span></span><br><span class="line">    <span class="number">2</span>/<span class="number">2</span>:        https:<span class="comment">//packagist.laravel-china.org/p/provider-2018-10$4f971ac341cf575b7ed47aa09773abb926ec0c2baad56a0da2c9eed868beb649.json</span></span><br><span class="line">    Finished: success: <span class="number">2</span>, <span class="attr">skipped</span>: <span class="number">0</span>, <span class="attr">failure</span>: <span class="number">0</span>, <span class="attr">total</span>: <span class="number">2</span></span><br><span class="line">Using version dev-master <span class="keyword">for</span> phpoffice/phpspreadsheet</span><br><span class="line">./composer.json has been updated</span><br><span class="line">Loading composer repositories <span class="keyword">with</span> package information</span><br><span class="line">Updating dependencies (including <span class="built_in">require</span>-dev)</span><br><span class="line">Your requirements could not be resolved to an installable set <span class="keyword">of</span> packages.</span><br><span class="line"></span><br><span class="line">  Problem <span class="number">1</span></span><br><span class="line">    - maatwebsite/excel <span class="number">3.0</span><span class="number">.9</span> requires phpoffice/phpspreadsheet ^<span class="number">1.2</span> -&gt; satisfiable by phpoffice/phpspreadsheet[<span class="number">1.2</span><span class="number">.0</span>, <span class="number">1.2</span><span class="number">.1</span>, <span class="number">1.3</span><span class="number">.0</span>, <span class="number">1.3</span><span class="number">.1</span>, <span class="number">1.4</span><span class="number">.0</span>, <span class="number">1.4</span><span class="number">.1</span>, <span class="number">1.5</span><span class="number">.0</span>] but these conflict <span class="keyword">with</span> your requirements or minimum-stability.</span><br><span class="line">    - maatwebsite/excel <span class="number">3.0</span><span class="number">.9</span> requires phpoffice/phpspreadsheet ^<span class="number">1.2</span> -&gt; satisfiable by phpoffice/phpspreadsheet[<span class="number">1.2</span><span class="number">.0</span>, <span class="number">1.2</span><span class="number">.1</span>, <span class="number">1.3</span><span class="number">.0</span>, <span class="number">1.3</span><span class="number">.1</span>, <span class="number">1.4</span><span class="number">.0</span>, <span class="number">1.4</span><span class="number">.1</span>, <span class="number">1.5</span><span class="number">.0</span>] but these conflict <span class="keyword">with</span> your requirements or minimum-stability.</span><br><span class="line">    - maatwebsite/excel <span class="number">3.0</span><span class="number">.9</span> requires phpoffice/phpspreadsheet ^<span class="number">1.2</span> -&gt; satisfiable by phpoffice/phpspreadsheet[<span class="number">1.2</span><span class="number">.0</span>, <span class="number">1.2</span><span class="number">.1</span>, <span class="number">1.3</span><span class="number">.0</span>, <span class="number">1.3</span><span class="number">.1</span>, <span class="number">1.4</span><span class="number">.0</span>, <span class="number">1.4</span><span class="number">.1</span>, <span class="number">1.5</span><span class="number">.0</span>] but these conflict <span class="keyword">with</span> your requirements or minimum-stability.</span><br><span class="line">    - Installation request <span class="keyword">for</span> maatwebsite/excel (locked at <span class="number">3.0</span><span class="number">.9</span>) -&gt; satisfiable by maatwebsite/excel[<span class="number">3.0</span><span class="number">.9</span>].</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Installation failed, reverting ./composer.json to its original content.</span><br><span class="line"></span><br><span class="line">suping3@BJ-D<span class="number">-212361</span>A MINGW64 /d/code/blog</span><br><span class="line">$ composer <span class="built_in">require</span> <span class="string">"phpoffice/phpspreadsheet:^1.2"</span></span><br><span class="line">    <span class="number">1</span>/<span class="number">3</span>:        https:<span class="comment">//packagist.laravel-china.org/p/provider-2018-10$edb575ac09ff566f26e69fb52521dbd7b5bd90cb0e97c11c04a834f1ba157b0b.json</span></span><br><span class="line">    <span class="number">2</span>/<span class="number">3</span>:        https:<span class="comment">//packagist.laravel-china.org/p/provider-latest$f7aba8aec5290950424531a654a2519842e07f0dcd995755aa8e64e7f622ad17.json</span></span><br><span class="line">    <span class="number">3</span>/<span class="number">3</span>:        https:<span class="comment">//packagist.laravel-china.org/p/provider-2018-07$1f1ac5377ce1027162f91a3db54217c36e38f7ae18744629e6e89c5e6d410c78.json</span></span><br><span class="line">    Finished: success: <span class="number">3</span>, <span class="attr">skipped</span>: <span class="number">0</span>, <span class="attr">failure</span>: <span class="number">0</span>, <span class="attr">total</span>: <span class="number">3</span></span><br><span class="line">./composer.json has been updated</span><br><span class="line">Loading composer repositories <span class="keyword">with</span> package information</span><br><span class="line">Updating dependencies (including <span class="built_in">require</span>-dev)</span><br><span class="line">    <span class="number">1</span>/<span class="number">1</span>:        https:<span class="comment">//dl.laravel-china.org/phpoffice/phpspreadsheet/2dfd06c59825914a1a325f2a2ed13634b9d8c411.zip</span></span><br><span class="line">    Finished: success: <span class="number">1</span>, <span class="attr">skipped</span>: <span class="number">0</span>, <span class="attr">failure</span>: <span class="number">0</span>, <span class="attr">total</span>: <span class="number">1</span></span><br></pre></td></tr></table></figure>
<h3 id="Fatal-error-Class-‘PHPUnit-Framework-TestCase’-not-found"><a href="#Fatal-error-Class-‘PHPUnit-Framework-TestCase’-not-found" class="headerlink" title="Fatal error: Class ‘PHPUnit_Framework_TestCase’ not found"></a>Fatal error: Class ‘PHPUnit_Framework_TestCase’ not found</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/dees040/laravel-api-responses/issues/2</span></span><br><span class="line">vi vendor/dees040/laravel-api-responses/tests/ResponseTest.php</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ResponseTest</span> <span class="keyword">extends</span> \<span class="title">PHPUnit</span>\<span class="title">Framework</span>\<span class="title">TestCase</span></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">$ <span class="title">phpunit</span> <span class="title">vendor</span>/<span class="title">dees040</span>/<span class="title">laravel</span>-<span class="title">api</span>-<span class="title">responses</span>/<span class="title">tests</span>/<span class="title">ResponseTest</span>.<span class="title">php</span></span></span><br><span class="line"><span class="class"><span class="title">PHPUnit</span> 6.5.3 <span class="title">by</span> <span class="title">Sebastian</span> <span class="title">Bergmann</span> <span class="title">and</span> <span class="title">contributors</span>.</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">..............                                                    14 / 14 (100%)</span></span><br><span class="line"><span class="class"></span></span><br><span class="line">Time: 378 ms, Memory: 16.00MB</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-的-collection"><a href="#Laravel-的-collection" class="headerlink" title="Laravel 的 collection"></a>Laravel 的 collection</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">$collection = <span class="keyword">new</span> \Illuminate\Database\Eloquent\Collection([<span class="string">'item a'</span>, <span class="string">'item b'</span>, <span class="string">'item c'</span>]);</span><br><span class="line">$collection = $collection-&gt;unique();</span><br><span class="line">执行时候会报错</span><br><span class="line"></span><br><span class="line">PHP Fatal error:  Call to a member <span class="function"><span class="keyword">function</span> <span class="title">getKey</span>(<span class="params"></span>) <span class="title">on</span> <span class="title">string</span> <span class="title">in</span> /<span class="title">Project</span>/<span class="title">nginx</span>/<span class="title">webbpm</span>-<span class="title">dev</span>.<span class="title">ssl</span>.<span class="title">lol</span>/<span class="title">vendor</span>/<span class="title">laravel</span>/<span class="title">framework</span>/<span class="title">src</span>/<span class="title">Illuminate</span>/<span class="title">Database</span>/<span class="title">Eloquent</span>/<span class="title">Collection</span>.<span class="title">php</span> <span class="title">on</span> <span class="title">line</span> 259</span></span><br><span class="line"><span class="function"></span></span><br><span class="line">  [Symfony\Component\Debug\Exception\FatalErrorException]</span><br><span class="line">  Call to a member <span class="function"><span class="keyword">function</span> <span class="title">getKey</span>(<span class="params"></span>) <span class="title">on</span> <span class="title">string</span></span></span><br><span class="line"><span class="function">这是因为 <span class="title">Illuminate</span>\<span class="title">Database</span>\<span class="title">Eloquent</span>\<span class="title">Collection</span> 通过调用自身的 <span class="title">getDictionary</span> 方法，使用根据主键去重复的逻辑覆盖了 <span class="title">Illuminate</span>\<span class="title">Support</span>\<span class="title">Collection</span> 的逻辑。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">以上代码改成<span class="title">https</span>://<span class="title">laravel</span>-<span class="title">china</span>.<span class="title">org</span>/<span class="title">topics</span>/18696</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">$collection</span> = <span class="title">collect</span>(<span class="params">[<span class="string">'item a'</span>, <span class="string">'item b'</span>, <span class="string">'item c'</span>]</span>);</span></span><br><span class="line"><span class="function"><span class="title">$collection</span> = <span class="title">$collection</span>-&gt;<span class="title">unique</span>(<span class="params"></span>);</span></span><br></pre></td></tr></table></figure>
<h3 id="使用”mews-captcha-2-0”-验证码图片不显示问题"><a href="#使用”mews-captcha-2-0”-验证码图片不显示问题" class="headerlink" title="使用”mews/captcha:~2.0” 验证码图片不显示问题"></a>使用”mews/captcha:~2.0” 验证码图片不显示问题</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">Symfony\Component\Debug\Exception\FatalThrowableError: Call to <span class="literal">undefined</span> <span class="function"><span class="keyword">function</span> <span class="title">Intervention</span>\<span class="title">Image</span>\<span class="title">Gd</span>\<span class="title">imagettfbbox</span>(<span class="params"></span>) <span class="title">in</span> <span class="title">file</span> /<span class="title">usr</span>/<span class="title">share</span>/<span class="title">nginx</span>/<span class="title">html</span>/<span class="title">LaraBBS</span>/<span class="title">vendor</span>/<span class="title">intervention</span>/<span class="title">image</span>/<span class="title">src</span>/<span class="title">Intervention</span>/<span class="title">Image</span>/<span class="title">Gd</span>/<span class="title">Font</span>.<span class="title">php</span> <span class="title">on</span> <span class="title">line</span> 85</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">如果已经开启了<span class="title">gd</span>，请参考下 <span class="title">https</span>://<span class="title">laravel</span>-<span class="title">china</span>.<span class="title">org</span>/<span class="title">articles</span>/18815?#<span class="title">reply69185</span> </span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">FROM</span> <span class="title">php</span>:7.0-<span class="title">fpm</span></span></span><br><span class="line"><span class="function"><span class="title">RUN</span> <span class="title">apt</span>-<span class="title">get</span> <span class="title">update</span> &amp;&amp; <span class="title">apt</span>-<span class="title">get</span> <span class="title">install</span> -<span class="title">y</span> \</span></span><br><span class="line"><span class="function"><span class="title">libfreetype6</span>-<span class="title">dev</span> \</span></span><br><span class="line"><span class="function"><span class="title">libjpeg62</span>-<span class="title">turbo</span>-<span class="title">dev</span> \</span></span><br><span class="line"><span class="function"><span class="title">libmcrypt</span>-<span class="title">dev</span> \</span></span><br><span class="line"><span class="function"><span class="title">libpng</span>-<span class="title">dev</span> \</span></span><br><span class="line"><span class="function">&amp;&amp; <span class="title">docker</span>-<span class="title">php</span>-<span class="title">ext</span>-<span class="title">install</span> -<span class="title">j$</span>(<span class="params">nproc</span>) <span class="title">iconv</span> <span class="title">mcrypt</span> \</span></span><br><span class="line"><span class="function">&amp;&amp; <span class="title">docker</span>-<span class="title">php</span>-<span class="title">ext</span>-<span class="title">configure</span> <span class="title">gd</span> --<span class="title">with</span>-<span class="title">freetype</span>-<span class="title">dir</span>=/<span class="title">usr</span>/<span class="title">include</span>/ --<span class="title">with</span>-<span class="title">jpeg</span>-<span class="title">dir</span>=/<span class="title">usr</span>/<span class="title">include</span>/ \</span></span><br><span class="line"><span class="function">&amp;&amp; <span class="title">docker</span>-<span class="title">php</span>-<span class="title">ext</span>-<span class="title">install</span> -<span class="title">j$</span>(<span class="params">nproc</span>) <span class="title">gd</span></span></span><br></pre></td></tr></table></figure>
<h3 id="在解析外部变量时的一个问题"><a href="#在解析外部变量时的一个问题" class="headerlink" title="在解析外部变量时的一个问题"></a>在解析外部变量时的一个问题</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://segmentfault.com/a/1190000017051950</span></span><br><span class="line">&lt;form method=<span class="string">"post"</span>&gt;</span><br><span class="line">    &lt;input type=<span class="string">"text"</span> name=<span class="string">"id[]"</span> value=<span class="string">"1"</span>&gt;</span><br><span class="line">    &lt;input type=<span class="string">"text"</span> name=<span class="string">"id[]_text"</span> value=<span class="string">"a"</span>&gt;</span><br><span class="line">    &lt;input type=<span class="string">"text"</span> name=<span class="string">"id[]"</span> value=<span class="string">"2"</span>&gt;</span><br><span class="line">    &lt;input type=<span class="string">"text"</span> name=<span class="string">"id[]_text"</span> value=<span class="string">"b"</span>&gt;</span><br><span class="line">    &lt;input type=<span class="string">"submit"</span>&gt;</span><br><span class="line">&lt;<span class="regexp">/form&gt;</span></span><br><span class="line"><span class="regexp">var_export($_POST);</span></span><br><span class="line"><span class="regexp">array (</span></span><br><span class="line"><span class="regexp">  'id' =&gt; </span></span><br><span class="line"><span class="regexp">  array (</span></span><br><span class="line"><span class="regexp">    0 =&gt; '1',</span></span><br><span class="line"><span class="regexp">    1 =&gt; 'a',</span></span><br><span class="line"><span class="regexp">    2 =&gt; '2',</span></span><br><span class="line"><span class="regexp">    3 =&gt; 'b',</span></span><br><span class="line"><span class="regexp">  ),</span></span><br><span class="line"><span class="regexp">)</span></span><br><span class="line"><span class="regexp">$foo["id"] = 1;</span></span><br><span class="line"><span class="regexp">$foo["id[]_text"] = 2;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">var_export($foo);</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">extract($foo);</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">var_export(get_defined_vars());</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-with-limit"><a href="#Laravel-with-limit" class="headerlink" title="Laravel with () limit"></a>Laravel with () limit</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">比如如何取得每篇文章中的前<span class="number">10</span>条评论</span><br><span class="line">下面为错误写法：https:<span class="comment">//laravel-china.org/articles/19932</span></span><br><span class="line"></span><br><span class="line">essay::<span class="keyword">with</span>([<span class="string">'comment'</span> =&gt; <span class="function"><span class="keyword">function</span>(<span class="params">$query</span>) </span>&#123;</span><br><span class="line">    $query-&gt;take(<span class="number">10</span>)</span><br><span class="line">&#125;])</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">products</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsToMany(Hproduct::<span class="class"><span class="keyword">class</span>,'<span class="title">hproduct_has_type</span>','<span class="title">type_id</span>','<span class="title">product_id</span>')</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">topTenproducts</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;products()-&gt;<span class="keyword">with</span>(<span class="string">'supplier'</span>)-&gt;where(<span class="string">'is_sale'</span>,<span class="string">'='</span>,<span class="number">1</span>)-&gt;limit(<span class="number">4</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">getProdcutsWithtopTenproducts</span>(<span class="params">$col</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $model = $<span class="keyword">this</span>-&gt;whereIn(<span class="string">'id'</span>,$col)-&gt;get();</span><br><span class="line">    $model-&gt;each(<span class="function"><span class="keyword">function</span> (<span class="params">$model</span>) </span>&#123;</span><br><span class="line">        $model-&gt;load(<span class="string">'topTenproducts'</span>);</span><br><span class="line">        &#125;);</span><br><span class="line">    <span class="keyword">return</span> $model-&gt;toArray();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="laravel-5-5-索引长度超过限制"><a href="#laravel-5-5-索引长度超过限制" class="headerlink" title="laravel 5.5 索引长度超过限制"></a>laravel 5.5 索引长度超过限制</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">laravel5<span class="number">.4</span>中改变了默认的编码类型(<span class="function"><span class="params">utf8</span> =&gt;</span> utf8mb4), 从而使email字段索引长度超过了的默认限制长度 https:<span class="comment">//laravel-china.org/articles/18306</span></span><br><span class="line">InnoDB 引擎中：</span><br><span class="line"></span><br><span class="line">uft8 varchar 最大长度 <span class="number">255</span></span><br><span class="line">utf8mb4 varchar 最大长度 <span class="number">191</span></span><br><span class="line">设置一下 varchar 的长度不超过 <span class="number">191</span> 即可</span><br><span class="line"> table-&gt;string(<span class="string">'email'</span>)-&gt;index(); varchar会使用utf8mb4编码, utf8mb4编码(<span class="number">4</span>bytes)属于utf8编码(<span class="number">3</span>bytes)的扩展类型,</span><br><span class="line">由此可以计算索引长度为<span class="number">255</span>*<span class="number">4</span>+<span class="number">2</span>=<span class="number">1022</span> &gt;<span class="number">767</span></span><br><span class="line">$ php artisan migrate</span><br><span class="line">Migration table created successfully.</span><br><span class="line"></span><br><span class="line">In Connection.php line <span class="number">664</span>:</span><br><span class="line"></span><br><span class="line">  SQLSTATE[<span class="number">42000</span>]: Syntax error or access violation: <span class="number">1071</span> Specified key was t</span><br><span class="line">  oo long; max key length is <span class="number">1000</span> bytes (SQL: alter table <span class="string">`users`</span> add unique</span><br><span class="line">  <span class="string">`users_email_unique`</span>(<span class="string">`email`</span>))</span><br></pre></td></tr></table></figure>
<h3 id="cron不执行"><a href="#cron不执行" class="headerlink" title="cron不执行"></a>cron不执行</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">坑<span class="number">1</span>：环境变量https:<span class="comment">//laravel-china.org/articles/18697</span></span><br><span class="line">当Cron无法生效时，可能是Cron执行环境变量不正确引起的</span><br><span class="line">PATH=<span class="regexp">/usr/</span>local/sbin:<span class="regexp">/usr/</span>local/bin:<span class="regexp">/usr/</span>sbin:<span class="regexp">/usr/</span>bin:<span class="regexp">/opt/my</span>sql/bin:<span class="regexp">/opt/</span>php7/bin:<span class="regexp">/opt/m</span>emcached/bin:<span class="regexp">/root/</span>bin</span><br><span class="line">* * * * * php /path-to-your-project/artisan schedule:run &gt;&gt; <span class="regexp">/dev/</span><span class="literal">null</span> <span class="number">2</span>&gt;&amp;<span class="number">1</span></span><br><span class="line">坑<span class="number">2</span>：Cron 执行用户导致 Laravel log 不可写</span><br><span class="line">通过 crontab -e 命令创建的 Cron 是属于 root 用户，如果定时任务在实行时主动写入日志或者遇到异常未捕捉，会创建 root 权限的日志文件，最终会导致 php-fpm 的 www 账号无法写入。</span><br><span class="line"></span><br><span class="line">因此需要在创建 cron 的时候指定用户</span><br><span class="line"></span><br><span class="line">crontab -u www -e</span><br><span class="line">坑<span class="number">3</span>：cron 内容最后一行未回车</span><br><span class="line"></span><br><span class="line">解决上述两点问题后，如果仍然发现 cron 不执行，请确认</span><br><span class="line"></span><br><span class="line">* * * * * php /path-to-your-project/artisan schedule:run &gt;&gt; <span class="regexp">/dev/</span><span class="literal">null</span> <span class="number">2</span>&gt;&amp;<span class="number">1</span></span><br></pre></td></tr></table></figure>
<h3 id="composer-update后输出Killed"><a href="#composer-update后输出Killed" class="headerlink" title="composer update后输出Killed"></a>composer update后输出Killed</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://laravel-china.org/articles/20443</span></span><br><span class="line">解决方法：</span><br><span class="line">在本地环境运行composer update</span><br><span class="line">上传新的composer.lock文件到服务器</span><br><span class="line">服务器上运行composer install</span><br><span class="line">这样就解决的依赖包更新的问题。</span><br></pre></td></tr></table></figure>
<h3 id="Memcached-get-could-not-unserialize-value-no-igbinary-support"><a href="#Memcached-get-could-not-unserialize-value-no-igbinary-support" class="headerlink" title="Memcached::get(): could not unserialize value, no igbinary support"></a>Memcached::get(): could not unserialize value, no igbinary support</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">./configure --enable-memcached --enable-memcached-igbinary --<span class="keyword">with</span>-php-config=</span><br><span class="line">/usr/local/bin/php-config</span><br><span class="line">但是很不幸，并没有如期待的那样编译成功，遇到如下错误:http:<span class="comment">//www.helpergarden.com/2017/11/php-memcached%E6%89%A9%E5%B1%95%E5%AE%89%E8%A3%85.html</span></span><br><span class="line"></span><br><span class="line"><span class="string">"./php_memcached.h:23:10: fatal error: 'Zend/zend_smart_str.h' file not found</span></span><br><span class="line"><span class="string">#include "</span>Zend/zend_smart_str.h<span class="string">""</span></span><br><span class="line"><span class="string">``</span><span class="string">` </span></span><br><span class="line"><span class="string">###  cURL version 7.10.5 or later is required to compile php with cURL support</span></span><br><span class="line"><span class="string">`</span><span class="string">``</span>javascript</span><br><span class="line">#./configure --with-php-config=/usr/local/php/bin/php-config</span><br><span class="line">configure: WARNING: You will need re2c <span class="number">0.13</span><span class="number">.4</span> or later <span class="keyword">if</span> you want to regenerate PHP parsers.</span><br><span class="line">checking <span class="keyword">for</span> gawk... gawk</span><br><span class="line">checking <span class="keyword">for</span> cURL support... yes, shared</span><br><span class="line">checking <span class="keyword">for</span> pkg-config... /usr/bin/pkg-config</span><br><span class="line">checking <span class="keyword">for</span> libcurl.pc... using <span class="keyword">default</span> path</span><br><span class="line">checking <span class="keyword">for</span> cURL <span class="number">7.10</span><span class="number">.5</span> or greater... configure: error: cURL version <span class="number">7.10</span><span class="number">.5</span> or later is required to compile php <span class="keyword">with</span> cURL support</span><br><span class="line">curl: <span class="keyword">try</span> <span class="string">'curl --help'</span> or <span class="string">'curl --manual'</span> <span class="keyword">for</span> more information</span><br><span class="line">[root@VM_0_14_centos curl]# yum install curl-devel</span><br><span class="line">Loaded plugins: fastestmirror, langpacks</span><br><span class="line">Repository epel is listed more than once <span class="keyword">in</span> the configuration</span><br><span class="line">carlwgeorge-ripgrep                                                                                               | <span class="number">3.0</span> kB  <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line"></span><br><span class="line">docker-ce-stable</span><br><span class="line">#./configure --with-php-config=/usr/local/php/bin/php-config</span><br><span class="line">#make &amp;&amp; make install</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//www.oschina.net/question/81181_19353?sort=default&amp;p=2</span></span><br></pre></td></tr></table></figure>
<h3 id="nginx-配置问题-FastCGI-sent-in-stderr-“Primary-script-unknown”"><a href="#nginx-配置问题-FastCGI-sent-in-stderr-“Primary-script-unknown”" class="headerlink" title="nginx 配置问题-FastCGI sent in stderr: “Primary script unknown”"></a>nginx 配置问题-FastCGI sent in stderr: “Primary script unknown”</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">一个关于 nginx 配置的问题，配置一个 php 网站，页面总是输出 File not found.，查看 nginx log 中报错：</span><br><span class="line">tail -f /<span class="keyword">var</span>/log/nginx/error.log</span><br><span class="line">FastCGI sent <span class="keyword">in</span> stderr: <span class="string">"Primary script unknown"</span> <span class="keyword">while</span> reading response header <span class="keyword">from</span> upstream</span><br><span class="line">[root@VM_0_14_centos ~]# cat /etc/nginx/conf.d/xss.conf</span><br><span class="line">server &#123;</span><br><span class="line">    listen       <span class="number">9696</span> default_server;</span><br><span class="line">    server_name  _;</span><br><span class="line">    root  /root/xssplatform/;</span><br><span class="line">    #root  /usr/share/nginx/html/xssplatform/;</span><br><span class="line"></span><br><span class="line">    rewrite <span class="string">"^/([0-9a-zA-Z]&#123;6&#125;)$"</span> /index.php?<span class="keyword">do</span>=code&amp;urlKey=$<span class="number">1</span> last;</span><br><span class="line">    rewrite <span class="string">"^/do/auth/(\w+?)(/domain/([\w\.]+?))?$"</span> /index.php?<span class="keyword">do</span>=<span class="keyword">do</span>&amp;auth=$<span class="number">1</span>&amp;domain=$<span class="number">3</span> last;</span><br><span class="line">    rewrite <span class="string">"^/register/(.*?)$"</span> /index.php?<span class="keyword">do</span>=register&amp;key=$<span class="number">1</span> last;</span><br><span class="line">    rewrite <span class="string">"^/register-validate/(.*?)$"</span> /index.php?<span class="keyword">do</span>=register&amp;act=validate&amp;key=$<span class="number">1</span> last;</span><br><span class="line"></span><br><span class="line">    location / &#123;</span><br><span class="line"></span><br><span class="line">        index index.html index.htm index.php;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    location ~ \.php$ &#123;</span><br><span class="line">        fastcgi_pass   <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">9005</span>;</span><br><span class="line">        fastcgi_index  index.php;</span><br><span class="line">        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;</span><br><span class="line">        include        fastcgi_params;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">fpm 用nobody用户运行  /root/xssplatform/ 权限也是nobody </span><br><span class="line">[root@VM_0_14_centos ~]# lsof -i:9005</span><br><span class="line">COMMAND   PID   USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME</span><br><span class="line">php-fpm <span class="number">13691</span>   root    <span class="number">7</span>u  IPv4 <span class="number">30712498</span>      <span class="number">0</span>t0  TCP VM_0_14_centos:<span class="number">9005</span> (LISTEN)</span><br><span class="line">php-fpm <span class="number">13692</span> nobody    <span class="number">0</span>u  IPv4 <span class="number">30712498</span>      <span class="number">0</span>t0  TCP VM_0_14_centos:<span class="number">9005</span> (LISTEN)</span><br><span class="line">php-fpm <span class="number">13693</span> nobody    <span class="number">0</span>u  IPv4 <span class="number">30712498</span>      <span class="number">0</span>t0  TCP VM_0_14_centos:<span class="number">9005</span> (LISTEN)</span><br><span class="line">https:<span class="comment">//www.v2ex.com/t/341441</span></span><br><span class="line"><span class="number">1.</span>给我看下你 php-fpm 的配置</span><br><span class="line"><span class="number">2.</span>请确定 /<span class="keyword">var</span>/run/php/php5<span class="number">.6</span>-fpm.sock 这个在 php-fpm 配置正确</span><br><span class="line"><span class="number">3.</span>注意 php 和 nginx 执行权限</span><br><span class="line">整个root目录树的权限都打开才可以</span><br><span class="line">mv /root/xssplatform/ <span class="regexp">/usr/</span>share/nginx/html/ 就好了</span><br></pre></td></tr></table></figure>
<h3 id="intervention-iamge下载图片过慢"><a href="#intervention-iamge下载图片过慢" class="headerlink" title="intervention/iamge下载图片过慢"></a>intervention/iamge下载图片过慢</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 记录开始时间</span></span><br><span class="line">$startTimestamp = microtime(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">$url = <span class="string">'http://wx.qlogo.cn/mmopen/XxT9TiaJ1ibf06TNRCMjQADS4opDHvQLguLZHpqkRlvuJYZicvJW4iaOalPsKIs0kpZ3F6864ZzibyObYiaucUQSrdp4pFTNDyIpxw/0'</span>;</span><br><span class="line"></span><br><span class="line">$avatar = \Image::make($url);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 记录结束时间https://segmentfault.com/a/1190000011989802</span></span><br><span class="line">$endTimestamp = microtime(<span class="literal">true</span>);</span><br><span class="line">$startTimestamp = microtime(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">$client = <span class="keyword">new</span> \GuzzleHttp\Client();</span><br><span class="line"></span><br><span class="line">$url = <span class="string">'http://wx.qlogo.cn/mmopen/XxT9TiaJ1ibf06TNRCMjQADS4opDHvQLguLZHpqkRlvuJYZicvJW4iaOalPsKIs0kpZ3F6864ZzibyObYiaucUQSrdp4pFTNDyIpxw/0'</span>;</span><br><span class="line"></span><br><span class="line">$avatarResponse = $client-&gt;get($url);</span><br><span class="line"></span><br><span class="line">$avatar = \Image::make($avatarResponse-&gt;getBody()-&gt;getContents());</span><br><span class="line"></span><br><span class="line">$endTimestamp = microtime(<span class="literal">true</span>);</span><br><span class="line">三次平均耗时在 <span class="number">0.07</span> 秒左右，和前面的 <span class="number">16</span> 秒相比，差了 <span class="number">200</span> 多倍</span><br></pre></td></tr></table></figure>
<h3 id="MySQL由一个双引号引发的血案"><a href="#MySQL由一个双引号引发的血案" class="headerlink" title="MySQL由一个双引号引发的血案"></a>MySQL由一个双引号引发的血案</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://mp.weixin.qq.com/s/TJsBPK-RbmvIS0PNJ8GqtQ</span></span><br><span class="line">mysql [localhost] &#123;msandbox&#125; (test) &gt; select id,str_col <span class="keyword">from</span> tbl_name where str_col=<span class="string">"xxx"</span> = <span class="string">"yyy"</span>;</span><br><span class="line">+----+---------+</span><br><span class="line">| id | str_col |</span><br><span class="line">+----+---------+</span><br><span class="line">|  <span class="number">1</span> | aaa     |</span><br><span class="line">|  <span class="number">2</span> | aaa     |</span><br><span class="line">|  <span class="number">3</span> | aaa     |</span><br><span class="line">|  <span class="number">4</span> | aaa     |</span><br><span class="line">+----+---------+</span><br><span class="line">mysql [localhost] &#123;msandbox&#125; (test) &gt; warnings</span><br><span class="line">Show warnings enabled.</span><br><span class="line">mysql [localhost] &#123;msandbox&#125; (test) &gt; explain extended select id,str_col <span class="keyword">from</span> tbl_name where str_col=<span class="string">"xxx"</span> = <span class="string">"yyy"</span>\G</span><br><span class="line">*************************** <span class="number">1.</span> row ***************************</span><br><span class="line">           id: <span class="number">1</span></span><br><span class="line">  select_type: SIMPLE</span><br><span class="line">        table: tbl_name</span><br><span class="line">         type: index</span><br><span class="line">possible_keys: NULL</span><br><span class="line">          key: idx_str</span><br><span class="line">      key_len: <span class="number">33</span></span><br><span class="line">          ref: NULL</span><br><span class="line">         rows: <span class="number">4</span></span><br><span class="line">     filtered: <span class="number">100.00</span></span><br><span class="line">        Extra: Using where; Using index</span><br><span class="line"><span class="number">1</span> row <span class="keyword">in</span> set, <span class="number">1</span> warning (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line">Note (Code <span class="number">1003</span>): <span class="comment">/* select#1 */</span> select <span class="string">`test`</span>.<span class="string">`tbl_name`</span>.<span class="string">`id`</span> AS <span class="string">`id`</span>,<span class="string">`test`</span>.<span class="string">`tbl_name`</span>.<span class="string">`str_col`</span> AS <span class="string">`str_col`</span> <span class="keyword">from</span> <span class="string">`test`</span>.<span class="string">`tbl_name`</span> where ((<span class="string">`test`</span>.<span class="string">`tbl_name`</span>.<span class="string">`str_col`</span> = <span class="string">'xxx'</span>) = <span class="string">'yyy'</span>)</span><br><span class="line">他把where条件转化成了 http:<span class="comment">//www.fordba.com/mysql-double-quotation-marks-accident.html </span></span><br><span class="line"></span><br><span class="line">((<span class="string">`test`</span>.<span class="string">`tbl_name`</span>.<span class="string">`str_col`</span> = <span class="string">'xxx'</span>) = <span class="string">'yyy'</span>)</span><br></pre></td></tr></table></figure>
<h3 id="foreach"><a href="#foreach" class="headerlink" title="foreach"></a>foreach</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line">$arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>];</span><br><span class="line">foreach($arr <span class="keyword">as</span> &amp;$value) &#123;</span><br><span class="line">    $value = $value * <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// $arr is now [2, 4, 6, 8]</span></span><br><span class="line">unset($value); <span class="comment">// 最后取消掉引用</span></span><br><span class="line"></span><br><span class="line">$value的引用仅在被遍历的数组可以被引用时才可用</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">  此段代码可以运行</span></span><br><span class="line"><span class="comment">  运行结果:</span></span><br><span class="line"><span class="comment">    1-2</span></span><br><span class="line"><span class="comment">    2-4</span></span><br><span class="line"><span class="comment">    3-6</span></span><br><span class="line"><span class="comment">    4-8</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">foreach (array(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>) <span class="keyword">as</span> &amp;$value) &#123;</span><br><span class="line">    echo $value, <span class="string">'-'</span>;</span><br><span class="line">    $value = $value * <span class="number">2</span>;</span><br><span class="line">    echo $value, PHP_EOL;</span><br><span class="line">&#125;</span><br><span class="line">foreach按照值进行循环的时候(by-value), foreach是对该数组的一个拷贝进行操作. 所以在循环过程中修改不影响循环结果</span><br><span class="line">$arr = [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>];</span><br><span class="line">$ref = &amp;$arr;</span><br><span class="line"></span><br><span class="line">foreach ($arr <span class="keyword">as</span> $val) &#123;</span><br><span class="line">    var_dump($val);</span><br><span class="line">    unset($arr[<span class="number">1</span>]);</span><br><span class="line">&#125;</span><br><span class="line">$arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"></span><br><span class="line">foreach ($arr <span class="keyword">as</span> $k =&gt; &amp;$v) &#123;</span><br><span class="line">    $v = $v * <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">foreach ($arr <span class="keyword">as</span> $k =&gt; $v) &#123;</span><br><span class="line">    echo $v, PHP_EOL;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出:</span></span><br><span class="line"><span class="comment">    2</span></span><br><span class="line"><span class="comment">    4</span></span><br><span class="line"><span class="comment">    4</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">$arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"></span><br><span class="line">foreach ($arr <span class="keyword">as</span> $k =&gt; &amp;$v) &#123;</span><br><span class="line">    $v = $v * <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line">unset($v);<span class="comment">//数组最后一个元素的 $value 引用在 foreach 循环之后仍会保留。建议使用 unset() 来将其销毁。</span></span><br><span class="line">foreach ($arr <span class="keyword">as</span> $k =&gt; $v) &#123;</span><br><span class="line">    echo $v, PHP_EOL;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出:</span></span><br><span class="line"><span class="comment">    2</span></span><br><span class="line"><span class="comment">    4</span></span><br><span class="line"><span class="comment">    6</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h3 id="proc-open-打开两个进程"><a href="#proc-open-打开两个进程" class="headerlink" title="proc_open()打开两个进程"></a>proc_open()打开两个进程</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://blog.jiaojie.site/_posts/2017/08/15/proc_open-and-bash/</span></span><br><span class="line">$descriptorspec = array(</span><br><span class="line">   <span class="number">0</span> =&gt; array(<span class="string">"pipe"</span>, <span class="string">"r"</span>),  <span class="comment">// stdin is a pipe that the child will read from</span></span><br><span class="line">   <span class="number">1</span> =&gt; array(<span class="string">"pipe"</span>, <span class="string">"w"</span>),  <span class="comment">// stdout is a pipe that the child will write to</span></span><br><span class="line">   <span class="number">2</span> =&gt; array(<span class="string">"file"</span>, <span class="string">"/tmp/error-output.txt"</span>, <span class="string">"a"</span>) <span class="comment">// stderr is a file to write to</span></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">$cwd = <span class="string">'/tmp'</span>;</span><br><span class="line">$env = array(<span class="string">'some_option'</span> =&gt; <span class="string">'aeiou'</span>);</span><br><span class="line"></span><br><span class="line">$process = proc_open(<span class="string">'sleep 100'</span>, $descriptorspec, $pipes, $cwd, $env);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (is_resource($process)) &#123;</span><br><span class="line">    <span class="comment">// $pipes now looks like this:</span></span><br><span class="line">    <span class="comment">// 0 =&gt; writeable handle connected to child stdin</span></span><br><span class="line">    <span class="comment">// 1 =&gt; readable handle connected to child stdout</span></span><br><span class="line">    <span class="comment">// Any error output will be appended to /tmp/error-output.txt</span></span><br><span class="line"></span><br><span class="line">    fwrite($pipes[<span class="number">0</span>], <span class="string">'&lt;?php print_r($_ENV); ?&gt;'</span>);</span><br><span class="line">    fclose($pipes[<span class="number">0</span>]);</span><br><span class="line"></span><br><span class="line">    echo stream_get_contents($pipes[<span class="number">1</span>]);</span><br><span class="line">    fclose($pipes[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// It is important that you close any pipes before calling</span></span><br><span class="line">    <span class="comment">// proc_close in order to avoid a deadlock</span></span><br><span class="line">    $return_value = proc_close($process);</span><br><span class="line"></span><br><span class="line">    echo <span class="string">"command returned $return_value\n"</span>;</span><br><span class="line">&#125;</span><br><span class="line">执行php sleep.php父进程下面竟然出现了两个进程</span><br><span class="line">PHP的proc_open系列函数在Linux操作系统中应该是通过系统默认的sh -c命令进行执行的，要把不同的sh带来的结果考虑进去</span><br><span class="line">将sh的软链接改到bash上面，PHP代码按照预期的结果执行下去了</span><br><span class="line">[root@VM_0_14_centos incubator-superset]# ll /bin/sh</span><br><span class="line">lrwxrwxrwx <span class="number">1</span> root root <span class="number">4</span> Mar <span class="number">19</span>  <span class="number">2018</span> /bin/sh -&gt; bash</span><br></pre></td></tr></table></figure>
<h3 id="MySQL大数据分页"><a href="#MySQL大数据分页" class="headerlink" title="MySQL大数据分页"></a>MySQL大数据分页</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//blog.jiaojie.site/_posts/2017/12/22/mysql-pagination-tips/</span></span><br><span class="line">SELECT * FROM test where status = <span class="number">1</span> limit start, <span class="number">1000</span></span><br><span class="line">select * <span class="keyword">from</span> test a , </span><br><span class="line">    (select id <span class="keyword">from</span> test where status = <span class="number">1</span> limit <span class="number">300000</span>, <span class="number">10</span>) b </span><br><span class="line">    where a.id  = b.id;</span><br><span class="line">select * <span class="keyword">from</span> test </span><br><span class="line">    inner join (select id <span class="keyword">from</span> test  limit <span class="number">300000</span>, <span class="number">10</span>) t2 </span><br><span class="line">    using (id);</span><br><span class="line">select * <span class="keyword">from</span> test </span><br><span class="line">    where status = <span class="number">1</span> and id &gt; xxx</span><br><span class="line">    order by id asc</span><br><span class="line">    limit <span class="number">10</span></span><br></pre></td></tr></table></figure>
<h3 id="每三位加个逗号分割"><a href="#每三位加个逗号分割" class="headerlink" title="每三位加个逗号分割"></a>每三位加个逗号分割</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">$input = <span class="string">"12345678"</span>;</span><br><span class="line"></span><br><span class="line">$regex = <span class="string">"#^(?&lt;first&gt;([0-9]&#123;1,3&#125;)??)(?&lt;others&gt;([0-9]&#123;3&#125;)*)$#"</span>;</span><br><span class="line"></span><br><span class="line">$callback = <span class="function"><span class="keyword">function</span>(<span class="params">$match</span>) </span>&#123;</span><br><span class="line">    $arr = str_split($match[<span class="string">"others"</span>], <span class="number">3</span>);</span><br><span class="line">    <span class="keyword">return</span> empty($match[<span class="string">"first"</span>]) ? (implode(<span class="string">","</span>, $arr)) : (<span class="string">""</span> . $match[<span class="string">"first"</span>] . <span class="string">","</span> . implode(<span class="string">","</span>, $arr));</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">$output = preg_replace_callback($regex, $callback, $input);</span><br><span class="line"></span><br><span class="line">var_dump($output);<span class="comment">//"12,345,678"</span></span><br></pre></td></tr></table></figure>
<h3 id="use-amp-Referance"><a href="#use-amp-Referance" class="headerlink" title="use &amp; Referance"></a>use &amp; Referance</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">use中传递php基本数据类型时，绝大多数情况都是传递的copy。</span><br><span class="line"></span><br><span class="line">这个大多数情况包括boolean/integer/float/string和array这样的类型。</span><br><span class="line"></span><br><span class="line">针对object的类型，传递的是引用，不需要加&amp;符号。</span><br><span class="line">$fib = <span class="function"><span class="keyword">function</span>(<span class="params">$n</span>)<span class="title">use</span>(<span class="params">&amp;$fib</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>($n == <span class="number">0</span> || $n == <span class="number">1</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> $fib($n - <span class="number">1</span>) + $fib($n - <span class="number">2</span>);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">echo $fib(<span class="number">10</span>);<span class="comment">//89</span></span><br><span class="line">$aaa = <span class="number">111</span>;</span><br><span class="line">$func = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) <span class="title">use</span>(<span class="params">$aaa</span>)</span>&#123; print $aaa; &#125;;</span><br><span class="line">$aaa = <span class="number">222</span>;</span><br><span class="line">$func(); <span class="comment">// Outputs "111"</span></span><br><span class="line">$aaa = <span class="number">111</span>;</span><br><span class="line">$func = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) <span class="title">use</span>(<span class="params">&amp;$aaa</span>)</span>&#123; print $aaa; &#125;;</span><br><span class="line">$aaa = <span class="number">222</span>;</span><br><span class="line">$func(); <span class="comment">// Outputs "222"</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Foo</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    public $foo = <span class="string">'default'</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$foo = <span class="keyword">new</span> Foo;</span><br><span class="line"></span><br><span class="line">$func = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) <span class="title">use</span> (<span class="params">$foo</span>) </span>&#123;</span><br><span class="line">    echo $foo-&gt;foo . <span class="string">"\n"</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">$foo-&gt;foo = <span class="string">'changable'</span>;</span><br><span class="line">$func();<span class="comment">// 输出 "changable"</span></span><br></pre></td></tr></table></figure>
<h3 id="判断-Eloqument-模型查询数据结果是否为空"><a href="#判断-Eloqument-模型查询数据结果是否为空" class="headerlink" title="判断 Eloqument 模型查询数据结果是否为空"></a>判断 Eloqument 模型查询数据结果是否为空</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> DB::table(<span class="string">'orders'</span>)-&gt;where(<span class="string">'finalized'</span>, <span class="number">1</span>)-&gt;exists();</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> DB::table(<span class="string">'orders'</span>)-&gt;where(<span class="string">'finalized'</span>, <span class="number">1</span>)-&gt;doesntExist();</span><br><span class="line"><span class="keyword">return</span> Users::where(<span class="string">'email'</span>,<span class="string">'xxx@qq.com'</span>)-&gt;exists();</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> Users::where(<span class="string">'email'</span>,<span class="string">'xxx@qq.com'</span>)-&gt;doesntExist();</span><br><span class="line">$users = User::where(<span class="string">'active'</span>, <span class="number">1</span>)-&gt;all();</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> $users-&gt;isEmpty();</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> $users-&gt;isNotEmpty();</span><br></pre></td></tr></table></figure>
<h3 id="requires-ext-pcntl"><a href="#requires-ext-pcntl" class="headerlink" title="requires ext-pcntl"></a>requires ext-pcntl</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">laravel/horizon v1<span class="number">.4</span><span class="number">.3</span> requires ext-pcntl * -&gt; the requested PHP extension pcntl is missing <span class="keyword">from</span> your system</span><br><span class="line">在composer.json中增加</span><br><span class="line"></span><br><span class="line"><span class="string">"config"</span>: &#123;</span><br><span class="line">        <span class="string">"preferred-install"</span>: <span class="string">"dist"</span>,</span><br><span class="line">        <span class="string">"sort-packages"</span>: <span class="literal">true</span>,</span><br><span class="line">        <span class="string">"optimize-autoloader"</span>: <span class="literal">true</span>,</span><br><span class="line">        <span class="string">"platform"</span>: &#123;</span><br><span class="line">            <span class="string">"ext-pcntl"</span>: <span class="string">"7.2"</span>,</span><br><span class="line">            <span class="string">"ext-posix"</span>: <span class="string">"7.2"</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br></pre></td></tr></table></figure>
<h3 id="exec函数被禁"><a href="#exec函数被禁" class="headerlink" title="exec函数被禁"></a>exec函数被禁</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line">为了保证数据的有序性，需要对同时消费的进程进行数量限制（同时消费队列的进程不得超过<span class="number">1</span>个）</span><br><span class="line">  public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $pid     = getmypid();</span><br><span class="line">        $pidFile = self::PID_FILE;</span><br><span class="line">        <span class="keyword">if</span> (file_exists($pidFile)) &#123;</span><br><span class="line">            $pid    = file_get_contents($pidFile);</span><br><span class="line">            $output = shell_exec(<span class="string">"ps aux | grep -v grep | grep &#123;$pid&#125; | grep php | grep index_prod | grep balabala"</span>);</span><br><span class="line">            <span class="keyword">if</span> (!empty($output)) &#123;</span><br><span class="line">                exit(<span class="number">0</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        file_put_contents($pidFile, $pid);</span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; <span class="number">100000</span>; $i++) &#123;</span><br><span class="line">            <span class="comment">// bla bla</span></span><br><span class="line">        &#125;</span><br><span class="line">        unlink($pidFile);</span><br><span class="line">    &#125;</span><br><span class="line">     public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;redis-&gt;exists(self::PID_KEY)) &#123;</span><br><span class="line">                retry_loop:$pid    = $<span class="keyword">this</span>-&gt;redis-&gt;get(self::PID_KEY);</span><br><span class="line">                $output = shell_exec(<span class="string">"ps aux | grep -v grep | grep &#123;$pid&#125; | grep php | grep index_prod | grep AsyncBatchMemberImport"</span>);</span><br><span class="line">                <span class="keyword">if</span> (!empty($output)) &#123;</span><br><span class="line">                    exit(<span class="number">0</span>);</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    $<span class="keyword">this</span>-&gt;redis-&gt;del(self::PID_KEY);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            $ret = $<span class="keyword">this</span>-&gt;redis-&gt;set(self::PID_KEY, getmypid(), array(<span class="string">'nx'</span>));</span><br><span class="line">            <span class="keyword">if</span> (<span class="literal">false</span> === $ret) &#123;</span><br><span class="line">                goto retry_loop;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; <span class="number">100000</span>; $i++) &#123;</span><br><span class="line">                <span class="comment">// bla bla</span></span><br><span class="line">            &#125;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;redis-&gt;del(self::PID_KEY);</span><br><span class="line">        &#125;</span><br><span class="line"> php script.php <span class="string">`cat /dev/urandom | head -n 10 | md5sum | head -c 10`</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params">$uniqueId = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;uniqueId = $uniqueId;</span><br><span class="line">        <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;redis-&gt;exists(self::PID_KEY)) &#123;</span><br><span class="line">            $pid = $<span class="keyword">this</span>-&gt;redis-&gt;get(self::PID_KEY);</span><br><span class="line">            log_warning(<span class="string">'ERP_IMPORT_ASYNC_INFO'</span>, <span class="string">"UniqueId &#123;$pid&#125; Still RUNNING"</span>);</span><br><span class="line">            exit(<span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        $ret = $<span class="keyword">this</span>-&gt;redis-&gt;set(self::PID_KEY, $uniqueId, array(<span class="string">'nx'</span>));</span><br><span class="line">        <span class="keyword">if</span> (<span class="literal">false</span> === $ret) &#123;</span><br><span class="line">            log_warning(<span class="string">'ERP_IMPORT_ASYNC_INFO'</span>, <span class="string">"Set PID FAILED, WILL RETRY, Current PID "</span> . $uniqueId);</span><br><span class="line">            exit(<span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;redis-&gt;expire(self::PID_KEY, <span class="number">360</span>);</span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; <span class="number">1000</span>; $i++) &#123;</span><br><span class="line">            <span class="comment">// bla bla</span></span><br><span class="line">        &#125;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;redis-&gt;del(self::PID_KEY);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="curl-用完千万别忘-close"><a href="#curl-用完千万别忘-close" class="headerlink" title="curl 用完千万别忘 close"></a>curl 用完千万别忘 close</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://laravel-china.org/articles/9172/curl-do-not-forget-close</span></span><br><span class="line">$ch = curl_init();</span><br><span class="line">curl_setopt($ch, CURLOPT_URL, $url); </span><br><span class="line">curl_setopt($ch, CURLOPT_RETURNTRANSFER, <span class="literal">true</span>);</span><br><span class="line">curl_setopt($ch, CURLOPT_HEADER, <span class="literal">false</span>);</span><br><span class="line">curl_setopt($ch, CURLOPT_POST, <span class="literal">true</span>);</span><br><span class="line">curl_setopt($ch, CURLOPT_POSTFIELDS, $data);</span><br><span class="line"><span class="keyword">try</span>&#123;</span><br><span class="line">    $content = curl_exec($ch);</span><br><span class="line">&#125; <span class="keyword">finally</span>&#123;</span><br><span class="line">    curl_close($ch);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Memcached-get-could-not-unserialize-value-no-igbinary-support-1"><a href="#Memcached-get-could-not-unserialize-value-no-igbinary-support-1" class="headerlink" title="Memcached::get(): could not unserialize value, no igbinary support"></a>Memcached::get(): could not unserialize value, no igbinary support</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//stackoverflow.com/questions/35200566/warning-memcachedgetmulti-could-not-unserialize-value-no-igbinary-support</span></span><br><span class="line">[root@localhost igbinary-2.0.8]# php7 -i|grep igbinary</span><br><span class="line">igbinary support =&gt; no</span><br><span class="line">PWD =&gt; <span class="regexp">/data1/</span>projects/suping/igbinary<span class="number">-2.0</span><span class="number">.8</span></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line">php -i | grep igbinary</span><br><span class="line">igbinary</span><br><span class="line">igbinary support =&gt; enabled</span><br><span class="line">igbinary version =&gt; <span class="number">2.0</span><span class="number">.1</span></span><br><span class="line">igbinary APC serializer ABI =&gt; <span class="number">0</span></span><br><span class="line">igbinary session support =&gt; yes</span><br><span class="line">igbinary.compact_strings =&gt; <span class="function"><span class="params">On</span> =&gt;</span> On</span><br><span class="line">igbinary support =&gt; yes</span><br><span class="line">memcached.serializer =&gt; <span class="function"><span class="params">igbinary</span> =&gt;</span> igbinary</span><br><span class="line">Available serializers =&gt; php, igbinary</span><br><span class="line">Registered serializer handlers =&gt; php php_binary igbinary</span><br><span class="line"></span><br><span class="line"> #wget http://pecl.php.net/get/igbinary-2.0.8.tgz</span><br><span class="line">-<span class="number">-2018</span><span class="number">-12</span><span class="number">-29</span> <span class="number">19</span>:<span class="number">58</span>:<span class="number">07</span>--  http:<span class="comment">//pecl.php.net/get/igbinary-2.0.8.tgz</span></span><br><span class="line">Resolving pecl.php.net... <span class="number">104.236</span><span class="number">.228</span><span class="number">.160</span></span><br><span class="line">Connecting to pecl.php.net|<span class="number">104.236</span><span class="number">.228</span><span class="number">.160</span>|:<span class="number">80.</span>.. connected.</span><br><span class="line">HTTP request sent, awaiting response... <span class="number">302</span> Found</span><br><span class="line">Location: http:<span class="comment">//120.52.51.17/pecl.php.net/get/igbinary-2.0.8.tgz [following]</span></span><br><span class="line">-<span class="number">-2018</span><span class="number">-12</span><span class="number">-29</span> <span class="number">19</span>:<span class="number">58</span>:<span class="number">08</span>--  http:<span class="comment">//120.52.51.17/pecl.php.net/get/igbinary-2.0.8.tgz</span></span><br><span class="line">Connecting to <span class="number">120.52</span><span class="number">.51</span><span class="number">.17</span>:<span class="number">80.</span>.. connected.</span><br><span class="line">HTTP request sent, awaiting response... <span class="number">200</span> OK</span><br><span class="line">Length: <span class="number">76708</span> (<span class="number">75</span>K) [application/octet-stream]</span><br><span class="line">Saving to: “igbinary<span class="number">-2.0</span><span class="number">.8</span>.tgz”</span><br><span class="line"></span><br><span class="line"><span class="number">100</span>%[================================================================================&gt;] <span class="number">76</span>,<span class="number">708</span>      <span class="number">1.75</span>K/s   <span class="keyword">in</span> <span class="number">55</span>s</span><br><span class="line"></span><br><span class="line"><span class="number">2018</span><span class="number">-12</span><span class="number">-29</span> <span class="number">19</span>:<span class="number">59</span>:<span class="number">12</span> (<span class="number">1.35</span> KB/s) - “igbinary<span class="number">-2.0</span><span class="number">.8</span>.tgz” saved [<span class="number">76708</span>/<span class="number">76708</span>]</span><br><span class="line">#tar -zxvf igbinary-2.0.8.tgz</span><br><span class="line"># cd igbinary-2.0.8</span><br><span class="line"># /data0/opt/php7/bin/phpize</span><br><span class="line">Configuring <span class="keyword">for</span>:</span><br><span class="line">PHP Api Version:         <span class="number">20151012</span></span><br><span class="line">Zend Module Api No:      <span class="number">20151012</span></span><br><span class="line">Zend Extension Api No:   <span class="number">320151012</span></span><br><span class="line"># ./configure --with-php-config=/data/php7/bin/php-config</span><br><span class="line">#make &amp;&amp; make install</span><br><span class="line">Build complete.</span><br><span class="line">Don<span class="string">'t forget to run '</span>make test<span class="string">'.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">Installing shared extensions:     /data0/opt/php7/lib/php/extensions/no-debug-zts-20151012/</span></span><br><span class="line"><span class="string">Installing header files:          /data0/opt/php7/include/php/</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-读取-ZIP乱码"><a href="#PHP-读取-ZIP乱码" class="headerlink" title="PHP 读取 ZIP乱码"></a>PHP 读取 ZIP乱码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://laravel-china.org/topics/21866</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取ZIP压缩文件内容列表</span></span><br><span class="line"><span class="comment"> * @param [string] $file ZIP文件路径</span></span><br><span class="line"><span class="comment"> * @param [string] $dir 需要列出的文件夹</span></span><br><span class="line"><span class="comment"> * @param [bool] $subdir 是否列出子文件夹内容</span></span><br><span class="line"><span class="comment"> * @param [callable] $callback 列出每一项时的回调函数</span></span><br><span class="line"><span class="comment"> * @return [Array]</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">zip_list_content</span>(<span class="params">$file, $dir=null, $subdir=true, $callback=null</span>) </span>&#123;</span><br><span class="line">    $zip = <span class="keyword">new</span> ZipArchive(); </span><br><span class="line">    <span class="keyword">if</span> (!$zip-&gt;open($file)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    $count = $zip-&gt;numFiles;</span><br><span class="line">    <span class="keyword">if</span> ($dir) $dir = trim($dir, <span class="string">'/'</span>);</span><br><span class="line">    <span class="keyword">if</span> (is_callable($callback) $_callback = <span class="literal">true</span>; <span class="keyword">else</span> $_callback = <span class="literal">false</span>;</span><br><span class="line">    $results = [];</span><br><span class="line">    <span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; $count; $i++) &#123;</span><br><span class="line">        $entry = $zip-&gt;statIndex($i, <span class="attr">ZipArchive</span>::FL_ENC_RAW);</span><br><span class="line">        $entry[<span class="string">'name'</span>] = rtrim(str_replace(<span class="string">'\\'</span>, <span class="string">'/'</span>, $entry[<span class="string">'name'</span>]), <span class="string">'/'</span>);</span><br><span class="line">        $encoding = mb_detect_encoding($entry[<span class="string">'name'</span>], array(<span class="string">'Shift_JIS'</span>,<span class="string">'EUC_JP'</span>,<span class="string">'EUC_KR'</span>,<span class="string">'KOI8-R'</span>,<span class="string">'ASCII'</span>,<span class="string">'GB2312'</span>,<span class="string">'GBK'</span>,<span class="string">'BIG5'</span>,<span class="string">'UTF-8'</span>));</span><br><span class="line">        $filename = iconv($encoding, <span class="string">'UTF-8'</span>, $entry[<span class="string">'name'</span>]);</span><br><span class="line">        $filename = $filename ?: $entry[<span class="string">'name'</span>];</span><br><span class="line">        $size = $entry[<span class="string">'size'</span>];</span><br><span class="line">        $comp_size = $entry[<span class="string">'comp_size'</span>];</span><br><span class="line">        $mtime = $entry[<span class="string">'mtime'</span>];</span><br><span class="line">        $crc = $entry[<span class="string">'crc'</span>];</span><br><span class="line">        $is_dir = ($crc == <span class="number">0</span>);</span><br><span class="line">        $path = <span class="string">'/'</span> . $filename;</span><br><span class="line">        <span class="keyword">if</span> ($dir and (stripos($filename, $dir) !== <span class="number">0</span> or strtolower($filename) == strtolower($dir))) <span class="keyword">continue</span>;</span><br><span class="line">        $_names = explode(<span class="string">'/'</span>, $filename);</span><br><span class="line">        $_idx = count($_names)<span class="number">-1</span>;</span><br><span class="line">        <span class="keyword">if</span> (!$subdir and $_idx != count(explode(<span class="string">'/'</span>, $dir))) <span class="keyword">continue</span>;</span><br><span class="line">        $name = $_names[$_idx];</span><br><span class="line">        $index = $i;</span><br><span class="line">        <span class="comment">//$data = $zip-&gt;getFromIndex($i);</span></span><br><span class="line">        $entry = compact(<span class="string">'name'</span>, <span class="string">'path'</span>, <span class="string">'size'</span>, <span class="string">'comp_size'</span>, <span class="string">'mtime'</span>, <span class="string">'crc'</span>, <span class="string">'index'</span>, <span class="string">'is_dir'</span>);</span><br><span class="line">        $results[] = $entry;</span><br><span class="line">        <span class="keyword">if</span> ($_callback) @call_user_func($callback, $entry, $index, $zip);</span><br><span class="line">    &#125;</span><br><span class="line">    $zip-&gt;close();</span><br><span class="line">    <span class="keyword">return</span> $results;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Memcached-get-could-not-unserialize-value-no-igbinary-support-2"><a href="#Memcached-get-could-not-unserialize-value-no-igbinary-support-2" class="headerlink" title="Memcached::get()could not unserialize value, no igbinary support"></a>Memcached::get()could not unserialize value, no igbinary support</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">使用laravel Cache::get()时报错，版本PHP7 ,报错文件 </span><br><span class="line">vendor/laravel/framework/src/Illuminate/Cache/MemcachedStore.php</span><br><span class="line">重新安装memcached,<span class="number">3.</span>x版本是支持php7的，而php5<span class="number">.2</span><span class="number">-5.6</span>需要使用<span class="number">2.</span>x版本</span><br><span class="line">$ wget https:<span class="comment">//codeload.github.com/php-memcached-dev/php-memcached/tar.gz/v3.1.3 </span></span><br><span class="line">$   tar -zxvf php-memcached<span class="number">-3.1</span><span class="number">.3</span>.tar.gz</span><br><span class="line">$ cd php-memcached<span class="number">-3.1</span><span class="number">.3</span> </span><br><span class="line">$/php7/bin/phpize</span><br><span class="line">Configuring <span class="keyword">for</span>:</span><br><span class="line">PHP Api Version: <span class="number">20151012</span></span><br><span class="line">Zend Module Api No: <span class="number">20151012</span></span><br><span class="line">Zend Extension Api No: <span class="number">320151012</span></span><br><span class="line"></span><br><span class="line">$ ./configure --enable-memcached --enable-memcached-igbinary --<span class="keyword">with</span>-php-config=<span class="regexp">/php7/</span>bin/php-config</span><br><span class="line">$ make &amp;&amp; make install</span><br><span class="line">Build complete.</span><br><span class="line">Don<span class="string">'t forget to run '</span>make test<span class="string">'.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">Installing shared extensions: /php7/lib/php/extensions/no-debug-zts-20151012/</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">$ php7 --ini</span></span><br><span class="line"><span class="string">Configuration File (php.ini) Path: /php7/etc/</span></span><br><span class="line"><span class="string">Loaded Configuration File: /php7/etc/php.ini</span></span><br><span class="line"><span class="string">Scan for additional .ini files in: (none)</span></span><br><span class="line"><span class="string">Additional .ini files parsed: (none)</span></span><br><span class="line"><span class="string">$ vi  /php7/etc/php.ini</span></span><br><span class="line"><span class="string">extension=memcache.so</span></span><br><span class="line"><span class="string">extension=/php7/lib/php/extensions/no-debug-zts-20151012/memcached.so</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">安装igbinary</span></span><br><span class="line"><span class="string">#wget http://pecl.php.net/get/igbinary-2.0.8.tgz</span></span><br><span class="line"><span class="string">#tar -zxvf igbinary-2.0.8.tgz</span></span><br><span class="line"><span class="string"># cd igbinary-2.0.8</span></span><br><span class="line"><span class="string"># /php7/bin/phpize</span></span><br><span class="line"><span class="string">Configuring for:</span></span><br><span class="line"><span class="string">PHP Api Version: 20151012</span></span><br><span class="line"><span class="string">Zend Module Api No: 20151012</span></span><br><span class="line"><span class="string">Zend Extension Api No: 320151012</span></span><br><span class="line"><span class="string"># ./configure --with-php-config=/php7/bin/php-config</span></span><br><span class="line"><span class="string">#make &amp;&amp; make install</span></span><br><span class="line"><span class="string">Build complete.</span></span><br><span class="line"><span class="string">Don'</span>t forget to run <span class="string">'make test'</span>.</span><br><span class="line"></span><br><span class="line">Installing shared extensions: <span class="regexp">/php7/</span>lib/php/extensions/no-debug-zts<span class="number">-20151012</span>/</span><br><span class="line">Installing header files: <span class="regexp">/php7/i</span>nclude/php/</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">参考http:<span class="comment">//www.runoob.com/memcached/php-connect-memcached.html</span></span><br></pre></td></tr></table></figure>
<p><a href="https://coffeephp.com/articles/10" target="_blank" rel="noopener">深入理解PHP之foreach</a></p>
<h3 id="laravel-redis-队列重复执行"><a href="#laravel-redis-队列重复执行" class="headerlink" title="laravel redis 队列重复执行"></a>laravel redis 队列重复执行</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br></pre></td><td class="code"><pre><span class="line">现象 https:<span class="comment">//suren1986.in/index.php/archives/44/</span></span><br><span class="line">有时跑一些很耗费时间的异步脚本，会在 artisan queue:listen 时通过 --timeout=<span class="number">3600</span> 设置长一点的时间，以避免超时。</span><br><span class="line">如果： <span class="number">1.</span> 使用 Redis 存放 queue 信息； <span class="number">2.</span> 跑了多个进程 artisan queue:listen --timeout=<span class="number">3600</span>；<span class="number">3.</span> 监听同一个 queue；<span class="number">4.</span> Job 的时间超过 <span class="number">60</span> 秒。就会发生同一个脚本被运行多次的情况。</span><br><span class="line"></span><br><span class="line"><span class="string">"LPOP"</span></span><br><span class="line"><span class="string">"queues:default"</span></span><br><span class="line"></span><br><span class="line">#取出任务后，先要放到 queues:default:reserved zset 中</span><br><span class="line"></span><br><span class="line"><span class="string">"ZADD"</span></span><br><span class="line"><span class="string">"queues:default:reserved"</span></span><br><span class="line"><span class="string">"1485386842"</span> </span><br><span class="line"><span class="string">"&#123;\"job\":\"Illuminate\\\\Queue\\\\CallQueuedHandler@call\",\"data\":&#123;\"commandName\":\"App\\\\Jobs\\\\ExampleJob\",\"command\":\"O:19:\\\"App\\\\Jobs\\\\ExampleJob\\\":7:&#123;s:17:\\\"\\u0000*\\u0000userIdentifier\\\";N;s:9:\\\"\\u0000*\\u0000realIp\\\";N;s:12:\\\"\\u0000*\\u0000requestId\\\";N;s:6:\\\"\\u0000*\\u0000job\\\";N;s:10:\\\"connection\\\";N;s:5:\\\"queue\\\";N;s:5:\\\"delay\\\";N;&#125;\"&#125;,\"id\":\"bwA7ICPqnjYiM0ErjRBNwn0kVWF6KeAs\",\"attempts\":1&#125;"</span></span><br><span class="line"></span><br><span class="line"># 任务执行完毕后， 从 queues:default:reserved zset 中删除</span><br><span class="line"></span><br><span class="line"><span class="string">"ZREM"</span></span><br><span class="line"><span class="string">"queues:default:reserved"</span></span><br><span class="line"><span class="string">"&#123;\"job\":\"Illuminate\\\\Queue\\\\CallQueuedHandler@call\",\"data\":&#123;\"commandName\":\"App\\\\Jobs\\\\ExampleJob\",\"command\":\"O:19:\\\"App\\\\Jobs\\\\ExampleJob\\\":7:&#123;s:17:\\\"\\u0000*\\u0000userIdentifier\\\";N;s:9:\\\"\\u0000*\\u0000realIp\\\";N;s:12:\\\"\\u0000*\\u0000requestId\\\";N;s:6:\\\"\\u0000*\\u0000job\\\";N;s:10:\\\"connection\\\";N;s:5:\\\"queue\\\";N;s:5:\\\"delay\\\";N;&#125;\"&#125;,\"id\":\"bwA7ICPqnjYiM0ErjRBNwn0kVWF6KeAs\",\"attempts\":1&#125;"</span></span><br><span class="line"></span><br><span class="line"># 如果任务失败，会放到 queue:default:delay zset 中</span><br><span class="line"></span><br><span class="line"><span class="string">"ZADD"</span></span><br><span class="line"><span class="string">"queues:default:delayed"</span></span><br><span class="line"><span class="string">"1485386783"</span> </span><br><span class="line"><span class="string">"&#123;\"job\":\"Illuminate\\\\Queue\\\\CallQueuedHandler@call\",\"data\":&#123;\"commandName\":\"App\\\\Jobs\\\\ExampleJob\",\"command\":\"O:19:\\\"App\\\\Jobs\\\\ExampleJob\\\":7:&#123;s:17:\\\"\\u0000*\\u0000userIdentifier\\\";N;s:9:\\\"\\u0000*\\u0000realIp\\\";N;s:12:\\\"\\u0000*\\u0000requestId\\\";N;s:6:\\\"\\u0000*\\u0000job\\\";N;s:10:\\\"connection\\\";N;s:5:\\\"queue\\\";N;s:5:\\\"delay\\\";N;&#125;\"&#125;,\"id\":\"uuPBCq4QE9ocnw8UbkLhUl2Lh07yPm6M\",\"attempts\":1&#125;"</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Pop the next job off of the queue.</span></span><br><span class="line"><span class="comment">	 *laravel 5.0</span></span><br><span class="line"><span class="comment">	 * @param  string  $queue</span></span><br><span class="line"><span class="comment">	 * @return \Illuminate\Contracts\Queue\Job|null</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	public <span class="function"><span class="keyword">function</span> <span class="title">pop</span>(<span class="params">$queue = null</span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		$original = $queue ?: $<span class="keyword">this</span>-&gt;<span class="keyword">default</span>;</span><br><span class="line"></span><br><span class="line">		$queue = $<span class="keyword">this</span>-&gt;getQueue($queue);</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> ( ! is_null($<span class="keyword">this</span>-&gt;expire))</span><br><span class="line">		&#123;</span><br><span class="line">			$<span class="keyword">this</span>-&gt;migrateAllExpiredJobs($queue);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		$job = $<span class="keyword">this</span>-&gt;getConnection()-&gt;lpop($queue);</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> ( ! is_null($job))</span><br><span class="line">		&#123;</span><br><span class="line">			$<span class="keyword">this</span>-&gt;getConnection()-&gt;zadd($queue.<span class="string">':reserved'</span>, $<span class="keyword">this</span>-&gt;getTime() + $<span class="keyword">this</span>-&gt;expire, $job);</span><br><span class="line"></span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">new</span> RedisJob($<span class="keyword">this</span>-&gt;container, $<span class="keyword">this</span>, $job, $original);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">         * Pop the next job off of the queue.</span></span><br><span class="line"><span class="comment">         *laravel 5.5</span></span><br><span class="line"><span class="comment">         * @param  string  $queue</span></span><br><span class="line"><span class="comment">         * @return \Illuminate\Contracts\Queue\Job|null</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">pop</span>(<span class="params">$queue = null</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;migrate($prefixed = $<span class="keyword">this</span>-&gt;getQueue($queue));</span><br><span class="line">    </span><br><span class="line">            list($job, $reserved) = $<span class="keyword">this</span>-&gt;retrieveNextJob($prefixed);</span><br><span class="line">    </span><br><span class="line">            <span class="keyword">if</span> ($reserved) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">new</span> RedisJob(</span><br><span class="line">                    $<span class="keyword">this</span>-&gt;container, $<span class="keyword">this</span>, $job,</span><br><span class="line">                    $reserved, $<span class="keyword">this</span>-&gt;connectionName, $queue ?: $<span class="keyword">this</span>-&gt;<span class="keyword">default</span></span><br><span class="line">                );</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">原因</span><br><span class="line">在 Laravel 的框架中，通过 Redis 获取下一个 Job 是通过 Illuminate\RedisQueue 完成的。</span><br><span class="line">获取下个 Job 的函数是 public <span class="function"><span class="keyword">function</span> <span class="title">pop</span>(<span class="params"></span>)。</span></span><br><span class="line"><span class="function">假设 <span class="title">queue</span> 的名称为 <span class="title">default</span>，<span class="title">Laravel</span> 在获取到脚本后，会将它放入 <span class="title">default</span>:<span class="title">reserved</span> 中。 <span class="title">default</span>:<span class="title">reserved</span> 的结构是 <span class="title">sorted</span> <span class="title">hash</span>，被放入的 <span class="title">Job</span> 会被赋予一个权值：<span class="title">time</span>(<span class="params"></span>) + <span class="title">this</span>-&gt;<span class="title">expire</span>，默认的 <span class="title">this</span>-&gt;<span class="title">expire</span> = 60，<span class="title">Laravel</span> 官方的配置中(<span class="params">config<span class="regexp">/queue.php)，也默认了 60 秒。</span></span></span></span><br><span class="line"><span class="function"><span class="params"><span class="regexp">这个函数在从 Redis queue 中 pop 一个数据前，会将所有过期的 Job 放回到原来的 queue 中。</span></span></span></span><br><span class="line"><span class="function"><span class="params"><span class="regexp">过期 Job 的来源包括 default:reserved，获取所有 default:reserved 中，权值小于当前时间的 Job。</span></span></span></span><br><span class="line"><span class="function"><span class="params"><span class="regexp">于是，超过 60 秒没有执行完成的 Job 被放入了 default queue 中，等待下一次执行。</span></span></span></span><br><span class="line"><span class="function"><span class="params"><span class="regexp">解决方案</span></span></span></span><br><span class="line"><span class="function"><span class="params"><span class="regexp">解决方法很简单，将 config/</span>queue.php，相应配置的 expire 设置为 null 即可。</span></span></span><br><span class="line"><span class="function"><span class="params">因为 Illuminate\RedisQueue 中，如果 expire 为 null，是不会做将将所有过期的 Job 放回到原来的 queue 中的操作的。</span></span></span><br><span class="line"><span class="function"><span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">安装上redisadmin后查看，发现添加的任务一直保留在队列中，没有被取走（删除）。并且这条任务的状态变为了reserved（保留的）。</span></span></span><br><span class="line"><span class="function"><span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">然后在网上发现了这篇文章(http:<span class="regexp">//y</span>ansu.org<span class="regexp">/2014/</span><span class="number">04</span><span class="regexp">/11/</span>redis-queue-in-laravel.html</span>)，它讲了<span class="title">laravel</span>使用<span class="title">redis</span>作为队列系统的执行流程。</span></span><br><span class="line"><span class="function">其中讲了什么情况下任务的状态会变为<span class="title">reserved</span>。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">在<span class="title">RedisQueue</span>:<span class="title">pop</span>(<span class="params"></span>)方法中，有这样一句：</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">$this</span>-&gt;<span class="title">redis</span>-&gt;<span class="title">zadd</span>(<span class="params">$queue.<span class="string">':reserved'</span>, $this-&gt;getTime(</span>) + 60, <span class="title">$job</span>);</span></span><br><span class="line"><span class="function">这里将当前执行的任务放到另外一个<span class="title">reserved</span>队列中，超时时间是60<span class="title">s</span>。也就是说，如果60<span class="title">s</span>后这个任务没有被删除掉，则任务会重新被放入队列中来。因此，在实际的使用过程中，任务很可能被多次执行。解决的办法是</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">SendEmail</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">fire</span>(<span class="params">$job, $data</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $job-&gt;<span class="keyword">delete</span>();</span><br><span class="line">        <span class="comment">// job </span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">retry_after配置选项和--timeout命令行选项是不一样的，但是可以同时工作来保证任务不会丢失并且不会重复执行。</span><br><span class="line"></span><br><span class="line">--timeout应该永远都要比retry_after短至少几秒钟的时间。这样就能保证任务进程总能在失败重试前就被杀死了。如果你的--timeout选项大于retry_after配置选项，你的任务可能被执行两次。</span><br><span class="line">https:<span class="comment">//phpartisan.cn/news/97.html</span></span><br><span class="line"></span><br><span class="line">对队列的重试次数做限制，超出限制后删除队列或延迟处理。</span><br><span class="line">通过对队列错误事件的监听我才知道，原来是由于在我的事件中使用了Model模型，再进入队列执行的时候，</span><br><span class="line">会使用Model的主键再去反查一次模型数据，而且使用的是firstOrFail方法，可能是由于之前的一些数据被物理删除</span><br><span class="line">了导致找不到的原因，这这里就报错了，而且没有删除的机制，所以队列一直在重试！</span><br><span class="line">使用Laravel队列时一定要记得监听</span><br><span class="line">Illuminate\Queue\Events\JobExceptionOccurred;</span><br><span class="line">Illuminate\Queue\Events\JobFailed;</span><br><span class="line">两个事件不然发生错误队列就删除不了了，如果是在执行队列中handle函数中</span><br><span class="line">一定要记得及时删除队列，使用<span class="keyword">delete</span>()方法即可！</span><br><span class="line"></span><br><span class="line">如果在执行handle的过程中出现抛出异常，那么本任务就算执行失败，就会将失败的任务存放到fail_jobs表当中。</span><br><span class="line"></span><br><span class="line">当然也可以手动释放任务，如下尝试执行超过<span class="number">2</span>次，就释放本次任务，不在存入fail_jobs表中。</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>($<span class="keyword">this</span>-&gt;attempts() &gt; <span class="number">2</span>)&#123;</span><br><span class="line">    $<span class="keyword">this</span>-&gt;release();</span><br><span class="line">&#125;</span><br><span class="line">被放入fail_jobs表中的任务是不会再执行了，重启之后和不会执行。</span><br><span class="line">此外我们可以在服务中注册Queue::failing来侦听失败时间，异步通知给开发人员</span><br><span class="line">https:<span class="comment">//www.omybug.com/2017/05/05/Laravel-%E9%98%9F%E5%88%97/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AppServiceProvider</span> <span class="keyword">extends</span> <span class="title">ServiceProvider</span></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        Queue::failing(<span class="function"><span class="keyword">function</span> (<span class="params">$connection, $job, $data</span>) </span>&#123;</span><br><span class="line">            <span class="comment">// Notify team of failing job...</span></span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">    ........</span><br><span class="line">&#125;</span><br><span class="line">在队列任务类上直接定义failed方法</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 处理失败任务</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">failed</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// Called when the job is failing...</span></span><br><span class="line">    &#125;</span><br><span class="line">要查看已插入到failed_jobs数据表中的所有失败任务，可以使用Artisan命令queue:failed：</span><br><span class="line"></span><br><span class="line">php artisan queue:failed</span><br><span class="line">+------+------------+---------+------------------------------------------+---------------------+</span><br><span class="line">| ID   | Connection | Queue   | Class                                    | Failed At           |</span><br><span class="line">+------+------------+---------+------------------------------------------+---------------------+</span><br><span class="line">| <span class="number">8766</span> | redis      | <span class="keyword">default</span> | Illuminate\Events\CallQueuedHandler@call | <span class="number">2019</span><span class="number">-01</span><span class="number">-09</span> <span class="number">13</span>:<span class="number">40</span>:<span class="number">42</span> |</span><br><span class="line">| <span class="number">8765</span> | redis      | <span class="keyword">default</span> | Illuminate\Events\CallQueuedHandler@call | <span class="number">2019</span><span class="number">-01</span><span class="number">-09</span> <span class="number">13</span>:<span class="number">37</span>:<span class="number">43</span> |</span><br><span class="line">| <span class="number">8764</span> | redis      | <span class="keyword">default</span> | Illuminate\Events\CallQueuedHandler@call | <span class="number">2019</span><span class="number">-01</span><span class="number">-09</span> <span class="number">13</span>:<span class="number">36</span>:<span class="number">49</span> </span><br><span class="line">该命令将会列出任务ID，连接，对列和失败时间，任务ID可用于重试失败任务，例如，要重试一个ID为<span class="number">5</span>的失败任务，要用到下面的命令：</span><br><span class="line"></span><br><span class="line">php artisan queue:retry <span class="number">5</span></span><br><span class="line">要重试所有失败任务，使用如下命令即可：</span><br><span class="line"></span><br><span class="line">php artisan queue:retry all</span><br><span class="line">如果你要删除一个失败任务，可以使用queue:forget命令：</span><br><span class="line"></span><br><span class="line">php artisan queue:forget <span class="number">5</span></span><br><span class="line">要删除所有失败任务，可以使用queue:flush命令：</span><br><span class="line"></span><br><span class="line">php artisan queue:flush</span><br></pre></td></tr></table></figure>
<h3 id="MySQL导入SQL出现MySQL-server-has-gone-away错误"><a href="#MySQL导入SQL出现MySQL-server-has-gone-away错误" class="headerlink" title="MySQL导入SQL出现MySQL server has gone away错误"></a>MySQL导入SQL出现MySQL server has gone away错误</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">MySQL导入SQL出现MySQL server has gone away错误</span><br><span class="line"></span><br><span class="line">项目中需要导入一个<span class="number">200</span>M的SQL文件，导入过程中会出现MySQL server has gone away错误</span><br><span class="line"></span><br><span class="line">查看一下允许导入的文件大小，默认是<span class="number">1</span>M</span><br><span class="line"></span><br><span class="line">show global variables like <span class="string">'max_allowed_packet'</span>;</span><br><span class="line">调整my.cnf中的值</span><br><span class="line">max_allowed_packet = <span class="number">512</span>M</span><br><span class="line"></span><br><span class="line">零时调整可以用，重启之后会失效</span><br><span class="line"></span><br><span class="line">set global max_allowed_packet=<span class="number">536870912</span>;</span><br></pre></td></tr></table></figure>
<h3 id="pdo查询出的数据均自动转换为字符串"><a href="#pdo查询出的数据均自动转换为字符串" class="headerlink" title="pdo查询出的数据均自动转换为字符串"></a>pdo查询出的数据均自动转换为字符串</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">PDO::ATTR_STRINGIFY_FETCHES 提取的时候将数值转换为字符串。 </span><br><span class="line">PDO::ATTR_EMULATE_PREPARES 启用或禁用预处理语句的模拟。</span><br><span class="line">在实例化medoo的时候在option参数设置中加入这两个选项，即可查询出原有数据类型：</span><br><span class="line">https:<span class="comment">//blog.cooooler.com/2018/12/15/%E4%BD%BF%E7%94%A8medoo%E6%9F%A5%E8%AF%A2%E5%87%BA%E7%9A%84%E6%95%B0%E6%8D%AE%E5%9D%87%E8%87%AA%E5%8A%A8%E8%BD%AC%E6%8D%A2%E4%B8%BA%E5%AD%97%E7%AC%A6%E4%B8%B2/</span></span><br><span class="line"><span class="string">'option'</span> =&gt; [</span><br><span class="line">		PDO::<span class="function"><span class="params">ATTR_CASE</span> =&gt;</span> PDO::CASE_NATURAL,</span><br><span class="line">		PDO::<span class="function"><span class="params">ATTR_STRINGIFY_FETCHES</span> =&gt;</span> <span class="literal">false</span>,</span><br><span class="line">		PDO::<span class="function"><span class="params">ATTR_EMULATE_PREPARES</span> =&gt;</span> <span class="literal">false</span></span><br><span class="line">	],</span><br></pre></td></tr></table></figure>
<h3 id="git-clone克隆速度过慢问题"><a href="#git-clone克隆速度过慢问题" class="headerlink" title="git clone克隆速度过慢问题"></a>git clone克隆速度过慢问题</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://blog.cooooler.com/2018/07/18/%E8%A7%A3%E5%86%B3git-clone%E5%85%8B%E9%9A%86%E9%80%9F%E5%BA%A6%E8%BF%87%E6%85%A2%E9%97%AE%E9%A2%98/</span></span><br><span class="line">git config --global http.proxy socks5:<span class="comment">//127.0.0.1:1080</span></span><br><span class="line">git config --global https.proxy socks5:<span class="comment">//127.0.0.1:1080</span></span><br></pre></td></tr></table></figure>
<h3 id="laravel中导出excel乱码"><a href="#laravel中导出excel乱码" class="headerlink" title="laravel中导出excel乱码"></a>laravel中导出excel乱码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://blog.cooooler.com/2018/05/11/PHP%E5%AF%BC%E5%87%BAexcel%E4%B9%B1%E7%A0%81%E7%9A%84%E7%BB%88%E6%9E%81%E8%A7%A3%E5%86%B3%E5%A4%A7%E6%B3%95/</span></span><br><span class="line">Excel::create($title, <span class="function"><span class="keyword">function</span> (<span class="params">$excel</span>) <span class="title">use</span> (<span class="params">$data, $type, $rows</span>) </span>&#123;</span><br><span class="line">        $excel-&gt;sheet(<span class="string">'导出excel'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$sheet</span>) <span class="title">use</span> (<span class="params">$data, $type, $rows</span>) </span>&#123;</span><br><span class="line">            $sheet-&gt;rows($data);</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 设置bom防止中文乱码 只需要在生成文件时输出BOM头即可。</span></span><br><span class="line">        echo chr(<span class="number">0xEF</span>) . chr(<span class="number">0xBB</span>) . chr(<span class="number">0xBF</span>);</span><br><span class="line"></span><br><span class="line">    &#125;)-&gt;<span class="keyword">export</span>($type);</span><br></pre></td></tr></table></figure>
<h3 id="laravel中进行PDF导出遇到的问题"><a href="#laravel中进行PDF导出遇到的问题" class="headerlink" title="laravel中进行PDF导出遇到的问题"></a>laravel中进行PDF导出遇到的问题</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//blog.cooooler.com/2018/08/29/%E6%80%BB%E7%BB%93%E4%B8%80%E4%B8%8Blaravel%E4%B8%AD%E8%BF%9B%E8%A1%8CPDF%E5%AF%BC%E5%87%BA%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98/</span></span><br><span class="line">composer <span class="built_in">require</span> barryvdh/laravel-snappy</span><br><span class="line"><span class="string">` php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider" `</span></span><br><span class="line">导出pdf中文乱码问题</span><br><span class="line"></span><br><span class="line">首先需要下载一个支持中文的字体包，比如微软雅黑</span><br><span class="line">然后在/usr/share/fonts目录中新建一个文件夹，例如文件夹名为chinese</span><br><span class="line">将下载好的字体文件移动到新建的文件夹中</span><br><span class="line">然后执行以下命令安装字体</span><br><span class="line">mkfontscale</span><br><span class="line">mkfontdir</span><br><span class="line">fc-cache</span><br></pre></td></tr></table></figure>
<h3 id="curl获取数据乱码"><a href="#curl获取数据乱码" class="headerlink" title="curl获取数据乱码"></a>curl获取数据乱码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> phlib/guzzle-middleware</span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 生成handler</span></span><br><span class="line">       $handler = \GuzzleHttp\HandlerStack::create();</span><br><span class="line">       $handler-&gt;push(<span class="keyword">new</span> \Phlib\Guzzle\ConvertCharset());</span><br><span class="line">       $handler-&gt;push(<span class="keyword">new</span> \Phlib\Guzzle\AbsoluteUrls());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 设置请求参数</span></span><br><span class="line">       $option = [</span><br><span class="line">           <span class="string">'timeout'</span> =&gt; <span class="number">10</span>,</span><br><span class="line">           <span class="string">'handler'</span> =&gt; $handler</span><br><span class="line">       ];</span><br><span class="line">      </span><br><span class="line">        <span class="comment">// 发起请求</span></span><br><span class="line">       $client = <span class="keyword">new</span> \GuzzleHttp\Client($option);</span><br><span class="line">       $response = $client-&gt;get(<span class="string">'需要请求的url'</span>);</span><br><span class="line"></span><br><span class="line">   &#125; <span class="keyword">catch</span> (\Exception $exception) &#123;</span><br><span class="line">       <span class="keyword">return</span> $exception-&gt;getMessage();</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   <span class="keyword">return</span> (string)$response-&gt;getBody();</span><br></pre></td></tr></table></figure>
<h3 id="mysql-emoji"><a href="#mysql-emoji" class="headerlink" title="mysql emoji"></a>mysql emoji</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">utf8_unicode_ci 只能存储 <span class="number">3</span> 个字节的字符，无法应对 Emoji 这种需要 <span class="number">4</span> 个字节的字符。https:<span class="comment">//suren1986.in/index.php/archives/45/</span></span><br><span class="line">弄清楚问题，解决起来就简单了，修改字符集为 utf8mb4。不过修改的时候又碰到 <span class="number">1</span> 个问题：utf8mb4_bin, utf8mb4_unicode_ci, utf8mb4_general_ci 应该选哪个？</span><br><span class="line"></span><br><span class="line">参考 MySQL 的 官方文档, ci 后缀表示不区分大小写(<span class="keyword">case</span> insensitive)，bin 因为通过二进制比较，所以区分大小写：参考 stackoverflow 的这篇 回答 unicode 在对很多语言来说排序和比较是准确的，但是相对慢一些，general 没有实现所有的 Unicode 排序规则，有些语言会有问题，但是快一些，不过到具体实践中，中文没什么差别，速度相差无几，我觉得用 unicode 更好一些，少留坑，</span><br><span class="line"></span><br><span class="line">还好我的表数据量不大，线上很快就转换完了，几百万数据就得半夜三更弄了。</span><br><span class="line"></span><br><span class="line">改完数据表还没结束，代码中连接数据库时指定的字符集也得改为 utf8mb4。</span><br></pre></td></tr></table></figure>
<h3 id="composer-install、update出错需要Token"><a href="#composer-install、update出错需要Token" class="headerlink" title="composer install、update出错需要Token"></a>composer install、update出错需要Token</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">报错信息：使用SSH Key做身份认证克隆失败，请输入你的Github凭证来访问私人的镜像。（翻译得不好，大概是这个意思）</span><br><span class="line"></span><br><span class="line">可是还是不懂这里需要填写的是什么，还是几番Google找到了答案。</span><br><span class="line"></span><br><span class="line">原来这里要输入的本地SSH生成的公钥,复制~<span class="regexp">/.ssh/i</span>d_rsa.pub内容粘贴到这里，回车就可以了</span><br></pre></td></tr></table></figure>
<h3 id="MISCONF-Redis-is-configured-to-save-RDB-snapshots"><a href="#MISCONF-Redis-is-configured-to-save-RDB-snapshots" class="headerlink" title="MISCONF Redis is configured to save RDB snapshots"></a>MISCONF Redis is configured to save RDB snapshots</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//laravel-china.org/articles/22025#fa405f</span></span><br><span class="line">有两种修改方法，一种是通过redis命令行修改，另一种是直接修改redis.conf配置文件</span><br><span class="line">命令行修改方式示例：</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; config set stop-writes-on-bgsave-error no</span><br><span class="line">修改redis.conf文件：</span><br><span class="line">vi打开redis-server配置的redis.conf文件，然后使用快捷匹配模式：/stop-writes-on-bgsave-error定位到stop-writes-on-bgsave-error字符串所在位置，接着把后面的yes设置为no即可。</span><br><span class="line"></span><br><span class="line">我用的命令行修改方式，</span><br><span class="line">docker exec -it <span class="number">0</span>d redis-cli <span class="number">0</span>d是我的redis容器</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; config set stop-writes-on-bgsave-error no</span><br></pre></td></tr></table></figure>
<h3 id="laravel的坑"><a href="#laravel的坑" class="headerlink" title="laravel的坑"></a>laravel的坑</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//www.jianshu.com/p/da9c0cae59ef</span></span><br><span class="line"><span class="number">1.</span>多数据库使用事务的坑（model指定了$connection，用的是/config/database.php里的连接）</span><br><span class="line">使用DB::beginTransaction()是没有效果的，相当于未使用事务，必须指定连接，事务才能生效</span><br><span class="line">DB::connection(<span class="string">'连接名'</span>)-&gt;beginTransaction()；</span><br><span class="line">DB::connection(<span class="string">'连接名'</span>)-&gt;rollback();</span><br><span class="line">DB::connection(<span class="string">'连接名'</span>)-&gt;commit();</span><br><span class="line"><span class="number">2.</span>主从分离中事务的坑</span><br><span class="line">在事务开始直到结束这一过程中，所有的查询更新删除操作都作用在主库，所以不必担心在事务中的查询会查询到从库，也没必要在事务中为了查询主库而使用onWriteConnection；</span><br><span class="line">未使用事务的地方，想要读主库的数据，可以使用onWriteConnection：  Table::onWriteConnection()-&gt;find($id);</span><br><span class="line"><span class="number">3.</span>increment与decrement的坑</span><br><span class="line">$rs1 = Order::find(<span class="number">41</span>)-&gt;decrement(<span class="string">'shipping_fee'</span>, <span class="number">0</span>);</span><br><span class="line">echo <span class="string">'$rs1 = '</span>.$rs1.<span class="string">' '</span>;</span><br><span class="line">$rs2 = Order::find(<span class="number">41</span>)-&gt;decrement(<span class="string">'order_amount'</span>, <span class="number">0</span>);</span><br><span class="line">echo <span class="string">'$rs2 = '</span>.$rs2.<span class="string">' '</span>;</span><br><span class="line">运行结果：$rs1 = <span class="number">1</span> $rs2 = <span class="number">0</span></span><br><span class="line">原因分析：查看这两行代码执行的真实sql语句：</span><br><span class="line">update orders set shipping_fee = shipping_fee - <span class="number">0</span>, updated_at = <span class="string">'2017-08-03 10:10:25'</span> where id = <span class="string">'41'</span></span><br><span class="line">update orders set order_amount = order_amount - <span class="number">0</span>, updated_at = <span class="string">'2017-08-03 10:10:25'</span> where id = <span class="string">'41'</span></span><br><span class="line">结论：因为使用了laravel时间自动更新的功能，而且更新的是同一条数据，updated_at为最新时间，第一条语句执行返回条数<span class="number">1</span>，而第二条的updated_at跟第一条一样，返回的更新条数就为<span class="number">0</span>了。</span><br></pre></td></tr></table></figure>
<h3 id="crontab"><a href="#crontab" class="headerlink" title="crontab"></a>crontab</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">新创建的cron job，不会马上执行，至少要过<span class="number">2</span>分钟才执行。如果重启cron则马上执行。</span><br><span class="line">当crontab失效时，可以尝试/etc/init.d/crond restart解决问题,或者查看日志看某个job有没有执行/报错tail -f /<span class="keyword">var</span>/log/cron</span><br></pre></td></tr></table></figure>
<h3 id="laravel-读操连接了读库和写库"><a href="#laravel-读操连接了读库和写库" class="headerlink" title="laravel 读操连接了读库和写库"></a>laravel 读操连接了读库和写库</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php</span></span><br><span class="line"><span class="comment"> * Establish a PDO connection based on the configuration.</span></span><br><span class="line"><span class="comment"> *https://laravel-china.org/topics/1879/laravel-5-configuration-read-and-write-separation-and-source-analysis</span></span><br><span class="line"><span class="comment"> * @param  array   $config</span></span><br><span class="line"><span class="comment"> * @param  string  $name</span></span><br><span class="line"><span class="comment"> * @return \Illuminate\Database\Connection</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">make</span>(<span class="params">array $config, $name = null</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $config = $<span class="keyword">this</span>-&gt;parseConfig($config, $name);</span><br><span class="line">    <span class="comment">// 如果配置了读写分离，则同时创建读库和写库的链接</span></span><br><span class="line">    <span class="keyword">if</span> (isset($config[<span class="string">'read'</span>])) &#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;createReadWriteConnection($config);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 如果没有配置，默认创建单个数据库链接</span></span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;createSingleConnection($config);</span><br><span class="line">&#125;</span><br><span class="line">protected <span class="function"><span class="keyword">function</span> <span class="title">createSingleConnection</span>(<span class="params">array $config</span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		$pdo = $<span class="keyword">this</span>-&gt;createConnector($config)-&gt;connect($config);dump($config);</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> $<span class="keyword">this</span>-&gt;createConnection($config[<span class="string">'driver'</span>], $pdo, $config[<span class="string">'database'</span>], $config[<span class="string">'prefix'</span>], $config);</span><br><span class="line">	&#125;</span><br><span class="line">	array:<span class="number">11</span> [▼</span><br><span class="line">      <span class="string">"driver"</span> =&gt; <span class="string">"mysql"</span></span><br><span class="line">      <span class="string">"port"</span> =&gt; <span class="string">"3306"</span></span><br><span class="line">      <span class="string">"host"</span> =&gt; <span class="string">"127.0.0.1"</span></span><br><span class="line">      <span class="string">"database"</span> =&gt; <span class="string">"mysql"</span></span><br><span class="line">      <span class="string">"username"</span> =&gt; <span class="string">"root"</span></span><br><span class="line">      <span class="string">"password"</span> =&gt; <span class="string">"root"</span></span><br><span class="line">      <span class="string">"charset"</span> =&gt; <span class="string">"utf8"</span></span><br><span class="line">      <span class="string">"collation"</span> =&gt; <span class="string">"utf8_unicode_ci"</span></span><br><span class="line">      <span class="string">"prefix"</span> =&gt; <span class="string">""</span></span><br><span class="line">      <span class="string">"strict"</span> =&gt; <span class="literal">false</span></span><br><span class="line">      <span class="string">"name"</span> =&gt; <span class="string">"read"</span></span><br><span class="line">    ]</span><br><span class="line">cat config/database.php</span><br><span class="line"><span class="string">'connections'</span> =&gt; [</span><br><span class="line"></span><br><span class="line">            <span class="string">'mysql'</span> =&gt; [</span><br><span class="line">                <span class="string">'driver'</span>    =&gt; <span class="string">'mysql'</span>,</span><br><span class="line">                <span class="string">'host'</span>      =&gt; env(<span class="string">'DB_HOST'</span>, <span class="string">'localhost'</span>),</span><br><span class="line">                <span class="string">'port'</span>      =&gt; env(<span class="string">'DB_PORT'</span>, <span class="number">3306</span>),</span><br><span class="line">                <span class="string">'database'</span>  =&gt; env(<span class="string">'DB_DATABASE'</span>, <span class="string">'forge'</span>),</span><br><span class="line">                <span class="string">'username'</span>  =&gt; env(<span class="string">'DB_USERNAME'</span>, <span class="string">'forge'</span>),</span><br><span class="line">                <span class="string">'password'</span>  =&gt; env(<span class="string">'DB_PASSWORD'</span>, <span class="string">''</span>),</span><br><span class="line">                <span class="string">'charset'</span>   =&gt; <span class="string">'utf8mb4'</span>,</span><br><span class="line">                <span class="string">'collation'</span> =&gt; <span class="string">'utf8mb4_unicode_ci'</span>,</span><br><span class="line">                <span class="string">'prefix'</span>    =&gt; env(<span class="string">'DB_PREFIX'</span>, <span class="string">'tbl_'</span>),</span><br><span class="line">                <span class="string">'timezone'</span>  =&gt; env(<span class="string">'DB_TIMEZONE'</span>, <span class="string">'+00:00'</span>),</span><br><span class="line">                <span class="string">'strict'</span>    =&gt; <span class="literal">false</span>,</span><br><span class="line">                <span class="string">'options'</span>   =&gt; array(</span><br><span class="line">                    PDO::<span class="function"><span class="params">ATTR_STRINGIFY_FETCHES</span> =&gt;</span> <span class="literal">true</span>,</span><br><span class="line">                    PDO::<span class="function"><span class="params">ATTR_EMULATE_PREPARES</span> =&gt;</span> <span class="literal">false</span></span><br><span class="line">                ),</span><br><span class="line">            ],</span><br><span class="line"></span><br><span class="line">            <span class="string">'mysql-read'</span> =&gt; [</span><br><span class="line">                <span class="string">'driver'</span>    =&gt; <span class="string">'mysql'</span>,</span><br><span class="line">                <span class="string">'host'</span>      =&gt; env(<span class="string">'DB_HOST_READ'</span>, <span class="string">'localhost'</span>),</span><br><span class="line">                <span class="string">'port'</span>      =&gt; env(<span class="string">'DB_PORT'</span>, <span class="number">3306</span>),</span><br><span class="line">                <span class="string">'database'</span>  =&gt; env(<span class="string">'DB_DATABASE'</span>, <span class="string">'forge'</span>),</span><br><span class="line">                <span class="string">'username'</span>  =&gt; env(<span class="string">'DB_USERNAME'</span>, <span class="string">'forge'</span>),</span><br><span class="line">                <span class="string">'password'</span>  =&gt; env(<span class="string">'DB_PASSWORD'</span>, <span class="string">''</span>),</span><br><span class="line">                <span class="string">'charset'</span>   =&gt; <span class="string">'utf8mb4'</span>,</span><br><span class="line">                <span class="string">'collation'</span> =&gt; <span class="string">'utf8mb4_unicode_ci'</span>,</span><br><span class="line">                <span class="string">'prefix'</span>    =&gt; env(<span class="string">'DB_PREFIX'</span>, <span class="string">'tbl_'</span>),</span><br><span class="line">                <span class="string">'timezone'</span>  =&gt; env(<span class="string">'DB_TIMEZONE'</span>, <span class="string">'+00:00'</span>),</span><br><span class="line">                <span class="string">'strict'</span>    =&gt; <span class="literal">false</span>,</span><br><span class="line">                <span class="string">'options'</span>   =&gt; array(</span><br><span class="line">                    PDO::<span class="function"><span class="params">ATTR_STRINGIFY_FETCHES</span> =&gt;</span> <span class="literal">true</span>,</span><br><span class="line">                    PDO::<span class="function"><span class="params">ATTR_EMULATE_PREPARES</span> =&gt;</span> <span class="literal">false</span></span><br><span class="line">                ),</span><br><span class="line">            ],</span><br><span class="line"></span><br><span class="line">            <span class="string">'mysql-write'</span> =&gt; [</span><br><span class="line">                <span class="string">'driver'</span>    =&gt; <span class="string">'mysql'</span>,</span><br><span class="line">                <span class="string">'host'</span>      =&gt; env(<span class="string">'DB_HOST_WRITE'</span>, <span class="string">'localhost'</span>),</span><br><span class="line">                <span class="string">'port'</span>      =&gt; env(<span class="string">'DB_PORT'</span>, <span class="number">3306</span>),</span><br><span class="line">                <span class="string">'database'</span>  =&gt; env(<span class="string">'DB_DATABASE'</span>, <span class="string">'forge'</span>),</span><br><span class="line">                <span class="string">'username'</span>  =&gt; env(<span class="string">'DB_USERNAME'</span>, <span class="string">'forge'</span>),</span><br><span class="line">                <span class="string">'password'</span>  =&gt; env(<span class="string">'DB_PASSWORD'</span>, <span class="string">''</span>),</span><br><span class="line">                <span class="string">'charset'</span>   =&gt; <span class="string">'utf8mb4'</span>,</span><br><span class="line">                <span class="string">'collation'</span> =&gt; <span class="string">'utf8mb4_unicode_ci'</span>,</span><br><span class="line">                <span class="string">'prefix'</span>    =&gt; env(<span class="string">'DB_PREFIX'</span>, <span class="string">'tbl_'</span>),</span><br><span class="line">                <span class="string">'timezone'</span>  =&gt; env(<span class="string">'DB_TIMEZONE'</span>, <span class="string">'+00:00'</span>),</span><br><span class="line">                <span class="string">'strict'</span>    =&gt; <span class="literal">false</span>,</span><br><span class="line">                <span class="string">'options'</span>   =&gt; array(</span><br><span class="line">                    PDO::<span class="function"><span class="params">ATTR_STRINGIFY_FETCHES</span> =&gt;</span> <span class="literal">true</span>,</span><br><span class="line">                    PDO::<span class="function"><span class="params">ATTR_EMULATE_PREPARES</span> =&gt;</span> <span class="literal">false</span></span><br><span class="line">                ),</span><br><span class="line">            ],</span><br><span class="line">        ],</span><br><span class="line">        <span class="class"><span class="keyword">class</span> <span class="title">DBConnService</span></span></span><br><span class="line"><span class="class">        </span>&#123;</span><br><span class="line">            private <span class="keyword">static</span> $read_conn = <span class="literal">null</span>;</span><br><span class="line">            private <span class="keyword">static</span> $write_conn = <span class="literal">null</span>;</span><br><span class="line">        </span><br><span class="line">            private <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line">        </span><br><span class="line">            public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">writeConn</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">                <span class="keyword">if</span> (self::$write_conn === <span class="literal">null</span>) &#123;</span><br><span class="line">                    self::$write_conn = DB::connection(<span class="string">'mysql-write'</span>);</span><br><span class="line">                &#125;</span><br><span class="line">        </span><br><span class="line">                <span class="keyword">return</span> self::$write_conn;</span><br><span class="line">            &#125;</span><br><span class="line">        </span><br><span class="line">            public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">readConn</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">                <span class="keyword">if</span> (self::$read_conn === <span class="literal">null</span>) &#123;</span><br><span class="line">                    self::$read_conn = DB::connection(<span class="string">'mysql-read'</span>);</span><br><span class="line">                &#125;</span><br><span class="line">        </span><br><span class="line">                <span class="keyword">return</span> self::$read_conn;</span><br><span class="line">            &#125;</span><br><span class="line">        </span><br><span class="line">        &#125;</span><br><span class="line">         $sql = <span class="string">"SELECT $fields FROM $table WHERE field = ? "</span>;</span><br><span class="line">            $result = DBConnService::readConn()-&gt;select($sql, [$field]);</span><br><span class="line">            <span class="comment">//$type = OrderTypes::find($type);</span></span><br><span class="line">             $type = OrderTypes::on(<span class="string">'mysql-read'</span>)-&gt;find($type);<span class="comment">//https://laravelacademy.org/post/9692.html</span></span><br><span class="line">        这样查询时只连接从库一次，而且一次请求中也只连接从库一次http:<span class="comment">//www.voidcn.com/article/p-dypdejbw-bew.html</span></span><br></pre></td></tr></table></figure>
<h3 id="502-Bad-Gateway"><a href="#502-Bad-Gateway" class="headerlink" title="502 Bad Gateway"></a>502 Bad Gateway</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"> [error] 26653#0: *748 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 10.235.51.48, server: xxx.cn, request: "GET /api/openapi.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "xx.cn"</span><br><span class="line">在php.ini和php-fpm.conf中分别有这样两个配置项：max_execution_time和request_terminate_timeout。</span><br><span class="line"></span><br><span class="line">这两项都是用来配置一个PHP脚本的最大执行时间的。当超过这个时间时，PHP-FPM不只会终止脚本的执行，</span><br><span class="line"></span><br><span class="line">还会终止执行脚本的Worker进程。所以Nginx会发现与自己通信的连接断掉了，就会返回给客户端<span class="number">502</span>错误。 request_terminate_timeout可以覆盖max_execution_time，所以如果不想改全局的php.ini，那只改PHP-FPM的配置就可以了。</span><br><span class="line">打开 php-fpm.conf 发现了：request_terminate_timeout = <span class="number">5</span></span><br><span class="line">request_terminate_timeout mixed</span><br><span class="line">设置单个请求的超时中止时间。该选项可能会对 php.ini 设置中的 ‘max_execution_time’ 因为某些特殊原因没有中止运行的脚本有用。设置为 ‘<span class="number">0</span>’ 表示 ‘Off’。可用单位：s（秒），m（分），h（小时）或者 d（天）。默认单位：s（秒）。默认值：<span class="number">0</span>（关闭）。</span><br><span class="line">估计就是这儿了 ，改成 <span class="number">30</span> 还是不行</span><br><span class="line">重启fpm 就好了</span><br><span class="line">[root@localhost ~]# cat fpm_restart.sh</span><br><span class="line"><span class="meta">#! /bin/bash</span></span><br><span class="line">PIDS=<span class="string">`ps aux | grep fpm | grep php | awk '&#123;print $2&#125;'`</span></span><br><span class="line"><span class="keyword">for</span> PID <span class="keyword">in</span> $PIDS</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">        kill <span class="number">-9</span> $PID</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">/data0/opt/php55/sbin/php-fpm</span><br><span class="line">https:<span class="comment">//blog.csdn.net/crj121624/article/details/79956283  </span></span><br><span class="line">如果你服务器并发量非常大，那只能先增加机器，然后按以下方式优化会取得更好效果;但如果你并发不大却出现<span class="number">502</span>，一般都可以归结为配置问题，脚本超时问题。</span><br></pre></td></tr></table></figure>
<h3 id="ffmpeg-command-not-found"><a href="#ffmpeg-command-not-found" class="headerlink" title="ffmpeg: command not found"></a>ffmpeg: command not found</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">通过PHP调用这个命令，没有引入对应的环境变量，导致找不到这个命令</span><br><span class="line"></span><br><span class="line">解决办法</span><br><span class="line"></span><br><span class="line">在PHP-fpm.conf的配置文件里面把下面几行前面的;去掉</span><br><span class="line">我的PHP配置文件php-fpm.conf 在/usr/local/php/etc/php-fpm.conf ;</span><br><span class="line">env[PATH]里面加入ffmpeg的路径，比如我的在/data/bin</span><br><span class="line"></span><br><span class="line">env[PATH] = <span class="regexp">/usr/</span>local/bin:<span class="regexp">/usr/</span>bin:<span class="regexp">/bin:/</span>data/bin</span><br><span class="line">env[TMP] = <span class="regexp">/tmp</span></span><br><span class="line"><span class="regexp">env[TMPDIR] = /</span>tmp</span><br><span class="line">env[TEMP] = <span class="regexp">/tmp</span></span><br><span class="line"><span class="regexp">env[LD_LIBRARY_PATH] = /u</span>sr/local/lib</span><br></pre></td></tr></table></figure>
<h3 id="Warning-simplexml-load-file"><a href="#Warning-simplexml-load-file" class="headerlink" title="Warning: simplexml_load_file()"></a>Warning: simplexml_load_file()</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Warning: simplexml_load_file(): I/O warning : failed to load external entity <span class="string">"xxx "</span></span><br><span class="line">后来将simplexml_load_file(<span class="string">"XXXX"</span>)替换成</span><br><span class="line">simplexml_load_string(file_get_contents(<span class="string">"XXXX"</span>));</span><br></pre></td></tr></table></figure>
<h3 id="同时读写一个文件的问题"><a href="#同时读写一个文件的问题" class="headerlink" title="同时读写一个文件的问题"></a>同时读写一个文件的问题</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">$fp = fopen(<span class="string">"/tmp/lock.txt"</span>,<span class="string">"w+"</span>);</span><br><span class="line">     <span class="keyword">if</span>(flock($fp, LOCK_EX))&#123;<span class="comment">// 进行排它型锁定http://www.bowen-tech.top/articles/detail/ar_5bc44ba823667_CJPBkHZN</span></span><br><span class="line">         fwrite($fp,<span class="string">"Write something here\n"</span>);</span><br><span class="line">         flock($fp, LOCK_UN);<span class="comment">// 释放锁定</span></span><br><span class="line">     &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">         echo <span class="string">"Couldn't lock the file !"</span>;</span><br><span class="line">     &#125;</span><br><span class="line">     fclose($fp);</span><br></pre></td></tr></table></figure>
<h3 id="nginx-error-open-“-run-nginx-pid”-failed"><a href="#nginx-error-open-“-run-nginx-pid”-failed" class="headerlink" title="nginx: [error] open() “/run/nginx.pid” failed"></a>nginx: [error] open() “/run/nginx.pid” failed</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_14_centos gogs]# ps aux|grep nginx</span><br><span class="line">root      <span class="number">7984</span>  <span class="number">0.0</span>  <span class="number">0.0</span>   <span class="number">4228</span>    <span class="number">24</span> ?        Ss   Jan21   <span class="number">0</span>:<span class="number">00</span> runsv nginx</span><br><span class="line">root      <span class="number">9479</span>  <span class="number">0.0</span>  <span class="number">0.0</span>   <span class="number">4372</span>     <span class="number">4</span> ?        S    Jan21   <span class="number">0</span>:<span class="number">00</span> svlogd -tt /<span class="keyword">var</span>/log/gitlab/nginx</span><br><span class="line">root     <span class="number">15221</span>  <span class="number">0.0</span>  <span class="number">0.0</span> <span class="number">112708</span>   <span class="number">972</span> pts/<span class="number">0</span>    R+   <span class="number">14</span>:<span class="number">42</span>   <span class="number">0</span>:<span class="number">00</span> grep --color=auto nginx</span><br><span class="line">root     <span class="number">24987</span>  <span class="number">0.0</span>  <span class="number">0.0</span>  <span class="number">38456</span>    <span class="number">36</span> ?        Ss   Jan22   <span class="number">0</span>:<span class="number">00</span> nginx: master process /opt/gitlab/embedded/sbin/nginx -p /<span class="keyword">var</span>/opt/gitlab/nginx</span><br><span class="line">gitlab-+ <span class="number">24993</span>  <span class="number">0.0</span>  <span class="number">0.0</span>  <span class="number">44792</span>    <span class="number">84</span> ?        S    Jan22   <span class="number">0</span>:<span class="number">00</span> nginx: worker process</span><br><span class="line">gitlab-+ <span class="number">24994</span>  <span class="number">0.0</span>  <span class="number">0.0</span>  <span class="number">40540</span>   <span class="number">120</span> ?        S    Jan22   <span class="number">0</span>:<span class="number">00</span> nginx: cache manager process</span><br><span class="line">[root@VM_0_14_centos gogs]# pkill -9 nginx</span><br><span class="line">[root@VM_0_14_centos gogs]# nginx -s reload</span><br><span class="line">nginx: [error] invalid PID number <span class="string">""</span> <span class="keyword">in</span> <span class="string">"/run/nginx.pid"</span></span><br><span class="line">nginx根本就没有启动过，所以pid文件的值为空没法平滑启动，先启动了才能平滑启动</span><br><span class="line">[root@VM_0_14_centos gogs]# nginx -c /etc/nginx/nginx.conf</span><br><span class="line">[root@VM_0_14_centos gogs]# nginx -s reload</span><br></pre></td></tr></table></figure>
<h3 id="502"><a href="#502" class="headerlink" title="502"></a>502</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">Nginx的超时配置</span><br><span class="line">fastcgi_connct_timeout <span class="number">60</span></span><br><span class="line">fastcgi_read_timeout <span class="number">300</span></span><br><span class="line">fastcgi_send_timeout <span class="number">300</span></span><br><span class="line"></span><br><span class="line">php.ini PHP-FPM 的超时配置</span><br><span class="line">max_execution_time <span class="number">300</span></span><br><span class="line">request_terminate_timeout <span class="number">0</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">pft_file_get_contents</span>(<span class="params">$url, $timeout = <span class="number">10</span>, $header = []</span>)</span>&#123;</span><br><span class="line">    $url     = strval($url);</span><br><span class="line">    $timeout = intval($timeout);</span><br><span class="line">    $timeout = $timeout &lt;= <span class="number">0</span> ? <span class="number">10</span> : $timeout;</span><br><span class="line"></span><br><span class="line">    $contextOptions = [</span><br><span class="line">        <span class="string">'http'</span> =&gt; [<span class="string">'timeout'</span> =&gt; $timeout]</span><br><span class="line">    ];</span><br><span class="line">    <span class="keyword">if</span>($header) &#123;</span><br><span class="line">        $contextOptions[<span class="string">'http'</span>][<span class="string">'header'</span>] = $header;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $context = stream_context_create($contextOptions);</span><br><span class="line">    $res = file_get_contents($url, <span class="literal">false</span>, $context);</span><br><span class="line">    <span class="keyword">return</span> $res;</span><br><span class="line">&#125;</span><br><span class="line">在Mysql和Redis服务器的配置中就会设置wait_timeout 和 timeout 参数，保证连接在超时没有连接的情况下断开连接。在程序方面需要做的就是处理超时后的<span class="string">'The MySQL server has gone away'</span> 和 <span class="string">'PHP Fatal error: Uncaught exception '</span>RedisException<span class="string">' with message '</span>read error on connection<span class="string">''</span> 的异常捕获和重连接处理。不过正常情况下，这些在底层框架应该都做了统一的封装。</span><br><span class="line">https:<span class="comment">//www.jianshu.com/p/3e3fb0459ca3</span></span><br><span class="line"> </span><br><span class="line">php.ini max_execution_time=<span class="number">1</span>，不一定<span class="number">1</span>s后就会中止脚本，可能是<span class="number">2</span>s、<span class="number">3</span>s甚至更长的时间；而request_terminate_timeout=<span class="number">4</span>则就会在<span class="number">4</span>s后中止脚本的执行。所以在配置超时时间的时候，最好两个都配置，max_execution_time时间短一点，而request_terminate_timeout时间长一点</span><br></pre></td></tr></table></figure>
<h3 id="php-a-无法使用"><a href="#php-a-无法使用" class="headerlink" title="php -a 无法使用"></a>php -a 无法使用</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://github.com/codcodog/Blog/issues/77</span></span><br><span class="line">编译安装的 PHP <span class="number">7.1</span><span class="number">.12</span>， 在命令行输入 php -a, 提示 Interactive mode enabled, 无法使用.</span><br><span class="line"></span><br><span class="line">解决办法：</span><br><span class="line">安装 readline 扩展.</span><br><span class="line"></span><br><span class="line">$ cd /usr/local/src/php<span class="number">-7.1</span><span class="number">.12</span>/ext/readline</span><br><span class="line">$ phpize</span><br><span class="line">$ ./configure --<span class="keyword">with</span>-php-config=<span class="regexp">/usr/</span>local/php/bin/php-config</span><br><span class="line">checking <span class="keyword">for</span> PHP installed headers prefix... /usr/local/php/include/php</span><br><span class="line">checking <span class="keyword">if</span> debug is enabled... no</span><br><span class="line">checking <span class="keyword">if</span> zts is enabled... no</span><br><span class="line">checking <span class="keyword">for</span> re2c... re2c</span><br><span class="line">checking <span class="keyword">for</span> re2c version... <span class="number">0.14</span><span class="number">.3</span> (ok)</span><br><span class="line">checking <span class="keyword">for</span> gawk... gawk</span><br><span class="line">checking <span class="keyword">for</span> libedit readline replacement... yes, shared</span><br><span class="line">configure: error: Please reinstall libedit - I cannot find readline.h</span><br><span class="line">$ make &amp;&amp; make install</span><br><span class="line">$ make clean</span><br><span class="line">编辑 php.ini， 添加 extension=readline.so 扩展.</span><br><span class="line"></span><br><span class="line">查看是否加载扩展</span><br><span class="line"></span><br><span class="line">$ php -m | grep readline</span><br><span class="line">readline</span><br></pre></td></tr></table></figure>
<h3 id="json-decode-返回null"><a href="#json-decode-返回null" class="headerlink" title="json_decode()返回null"></a>json_decode()返回null</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">json_decode()函数，要求输入的字符串是有要求的：</span><br><span class="line"></span><br><span class="line">使用UTF<span class="number">-8</span>编码</span><br><span class="line">不能在最后元素有逗号</span><br><span class="line">不能使用单引号</span><br><span class="line">不能有\r,\t</span><br><span class="line">所以，在接口传过来的json字符串中，需要查看是否包含了BOM头，如果有，去除</span><br></pre></td></tr></table></figure>
<h3 id="env忽略"><a href="#env忽略" class="headerlink" title="env忽略"></a>env忽略</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">在env中为DB_PASSWORD=abcde#142!*，但是在PHP代码中读取的数据库密码配置为abcde</span><br><span class="line">.env文件中，对密码字段加上双引号,如DB_PASSWORD=<span class="string">"abcde#142!*"</span>，然后一切恢复正常。</span><br></pre></td></tr></table></figure>
<h3 id="exceeded-the-timeout-of-60-seconds"><a href="#exceeded-the-timeout-of-60-seconds" class="headerlink" title="exceeded the timeout of 60 seconds"></a>exceeded the timeout of 60 seconds</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[Symfony\Component\Process\Exception\ProcessTimedOutException]                                                                                                                                                                              </span><br><span class="line">The process <span class="string">""</span>/usr/local/Cellar/php55/<span class="number">5.5</span><span class="number">.14</span>/bin/php<span class="string">" artisan queue:work  </span></span><br><span class="line"><span class="string">--queue="</span>QUEUE_URL<span class="string">" --delay=0 --memory=128 --sleep=3 --tries=0"</span> </span><br><span class="line">exceeded the timeout <span class="keyword">of</span> <span class="number">60</span> seconds.</span><br><span class="line">https:<span class="comment">//stackoverflow.com/questions/25877752/laravel-queue-process-timeout-error/26238686 </span></span><br><span class="line">php artisan queue:listen --timeout=<span class="number">0</span></span><br></pre></td></tr></table></figure>
<h3 id="set-exception-handler-不再保证收到的一定是-Exception-对象"><a href="#set-exception-handler-不再保证收到的一定是-Exception-对象" class="headerlink" title="set_exception_handler() 不再保证收到的一定是 Exception 对象"></a>set_exception_handler() 不再保证收到的一定是 Exception 对象</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line"><span class="comment">// PHP 5 时代的代码将会出现问题</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">handler</span>(<span class="params">Exception $e</span>) </span>&#123; ... &#125;</span><br><span class="line">set_exception_handler(<span class="string">'handler'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 兼容 PHP 5 和 7 http://www.syyong.com/php/Php5-5-upgrade-to-php7-2.html</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">handler</span>(<span class="params">$e</span>) </span>&#123; ... &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 仅支持 PHP 7</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">handler</span>(<span class="params">Throwable $e</span>) </span>&#123; ... &#125;</span><br></pre></td></tr></table></figure>
<h3 id="二维码生成乱码"><a href="#二维码生成乱码" class="headerlink" title="二维码生成乱码"></a>二维码生成乱码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> endroid/qr-code</span><br><span class="line">use Endroid\QrCode\QrCode;</span><br><span class="line"></span><br><span class="line">$qrCode = <span class="keyword">new</span> QrCode(<span class="string">'Life is too short to be generating QR codes'</span>);</span><br><span class="line"></span><br><span class="line">header(<span class="string">'Content-Type: '</span>.$qrCode-&gt;getContentType());</span><br><span class="line">echo $qrCode-&gt;writeString();</span><br><span class="line"><span class="comment">//加上die;才能访问 https://learnku.com/articles/22850?#reply80905</span></span><br><span class="line">die;</span><br></pre></td></tr></table></figure>
<h3 id="yum-Error-rpmdb-open-failed"><a href="#yum-Error-rpmdb-open-failed" class="headerlink" title="yum Error: rpmdb open failed"></a>yum Error: rpmdb open failed</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"> yum install net-tools</span><br><span class="line">error: rpmdb: BDB0113 Thread/process <span class="number">23439</span>/<span class="number">140210621216832</span> failed: BDB1507 Thread died <span class="keyword">in</span> Berkeley DB library</span><br><span class="line">error: db5 error(<span class="number">-30973</span>) <span class="keyword">from</span> dbenv-&gt;failchk: BDB0087 DB_RUNRECOVERY: Fatal error, run database recovery</span><br><span class="line">error: cannot open Packages index using db5 -  (<span class="number">-30973</span>)</span><br><span class="line">error: cannot open Packages database <span class="keyword">in</span> /<span class="keyword">var</span>/lib/rpm</span><br><span class="line">CRITICAL:yum.main:</span><br><span class="line"></span><br><span class="line"><span class="built_in">Error</span>: rpmdb open failed</span><br><span class="line">由于强制结束了yum 操作而导致rpm数据库被损坏了！</span><br><span class="line"><span class="number">1.</span> 进入目录， 查看相关rpmdb文件</span><br><span class="line"># cd /var/lib/rpm/ </span><br><span class="line"># ls | grep 'db.'  </span><br><span class="line">   __db<span class="number">.001</span></span><br><span class="line">   __db<span class="number">.002</span></span><br><span class="line">   __db<span class="number">.003</span></span><br><span class="line">   __db<span class="number">.004</span></span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 删除损坏的rpmdb文件</span><br><span class="line">将原rpmdb文件都更名为结尾带.bak的文件</span><br><span class="line"># for i in `ls | grep 'db.'`;do mv $i $i.bak;done</span><br><span class="line">或</span><br><span class="line"># rm -f __db.* # 清除原rpmdb文件</span><br><span class="line"></span><br><span class="line"><span class="number">3.</span> 重建rpm数据库</span><br><span class="line"># rpm --rebuilddb </span><br><span class="line"></span><br><span class="line"><span class="number">4.</span> 清除所有yum的缓存</span><br><span class="line"># yum clean all</span><br></pre></td></tr></table></figure>
<h3 id="泄露-env-配置"><a href="#泄露-env-配置" class="headerlink" title="泄露 env 配置"></a>泄露 env 配置</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Laravel 默认依赖的 filp/whoops包 ，当 .env 文件中配置 APP_DEBUG=<span class="literal">true</span> 时，会在出错页面打印 .env 配置信息。</span><br><span class="line">Laravel 常用的 barryvdh/laravel-debugbar 包，当 .env 文件中配置 APP_DEBUG=<span class="literal">true</span> 时，会输出 session 等敏感信息。</span><br><span class="line">以上两个包默认都在 composer.json 的 <span class="built_in">require</span>-dev 分支里面，生产环境不安装这两个包，就算 APP_DEBUG 配置成 <span class="literal">true</span>，也不会泄露太多敏感的信息。</span><br><span class="line"></span><br><span class="line">所以，生产环境部署系统时，一定要加 --no-dev：</span><br><span class="line"></span><br><span class="line">composer install --no-dev -vvv</span><br><span class="line">切记：不要让别人能直接访问到 .env 文件</span><br></pre></td></tr></table></figure>
<h3 id="laravel-路由冲突"><a href="#laravel-路由冲突" class="headerlink" title="laravel 路由冲突"></a>laravel 路由冲突</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//restful 查看</span></span><br><span class="line">Route::get(<span class="string">'products/&#123;product&#125;'</span>, <span class="string">'ProductsController@show'</span>)-&gt;name(<span class="string">'products.show'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//收藏</span></span><br><span class="line">Route::get(<span class="string">'products/favorites'</span>, <span class="string">'ProductsController@favorites'</span>)-&gt;name(<span class="string">'products.favorites'</span>);</span><br><span class="line">教程中给出的一种解决方式是将两个路由的位置进行上下调整，当项目变的越来越大，路由增多后，这样的操作可能会给后期挖坑</span><br><span class="line"></span><br><span class="line">所以建议增加where条件限制</span><br><span class="line"></span><br><span class="line"><span class="comment">//restful 查看，product仅支持数整，当id规则变更后，只需更改正则条件</span></span><br><span class="line">Route::get(<span class="string">'products/&#123;product&#125;'</span>, <span class="string">'ProductsController@show'</span>)-&gt;name(<span class="string">'products.show'</span>)-&gt;where([<span class="string">'product'</span> =&gt; <span class="string">'[0-9]+'</span>]);</span><br></pre></td></tr></table></figure>
<h3 id="No-‘Access-Control-Allow-Origin’-header-is-present-on-the-requested-resource"><a href="#No-‘Access-Control-Allow-Origin’-header-is-present-on-the-requested-resource" class="headerlink" title="No ‘Access-Control-Allow-Origin’ header is present on the requested resource"></a>No ‘Access-Control-Allow-Origin’ header is present on the requested resource</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">发送非简单请求时，伺服器端会先收到一个 OPTIONS 的预请求，前端只有收到这个预请求的正常回应，才会发送正式的 POST 请求。</span><br><span class="line">新增中间件 app\Http\Middleware\Cors.php，处理 POST 请求</span><br><span class="line"> public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">$request, Closure $next</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $response = $next($request);</span><br><span class="line">        <span class="keyword">if</span> ($request-&gt;getMethod() === <span class="string">'OPTIONS'</span>) &#123;</span><br><span class="line">                    $response-&gt;header(<span class="string">'Access-Control-Allow-Origin'</span>, <span class="string">'*'</span>);</span><br><span class="line">                    $response-&gt;header(<span class="string">'Access-Control-Allow-Methods'</span>, <span class="string">'GET, POST, PUT, PATCH, DELETE, OPTIONS'</span>);</span><br><span class="line">                    $response-&gt;header(<span class="string">'Access-Control-Allow-Credentials'</span>, <span class="string">'true'</span>);</span><br><span class="line">                    $response-&gt;header(<span class="string">'Access-Control-Allow-Headers'</span>, <span class="string">'Authorization, Content-Type, X-PINGOTHER'</span>);</span><br><span class="line">                &#125;</span><br><span class="line">        $response-&gt;header(<span class="string">'Access-Control-Allow-Origin'</span>, <span class="string">'*'</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $response;</span><br><span class="line">    &#125;</span><br><span class="line">    protected $routeMiddleware = [</span><br><span class="line">        ...</span><br><span class="line">    </span><br><span class="line">        <span class="string">'cors'</span> =&gt; \App\Http\Middleware\Cors::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">    ];</span><br><span class="line">Route::post(<span class="string">'upload'</span>, <span class="string">'UploadController@upload'</span>)-&gt;middleware(<span class="string">'cors'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="Git-status中文乱码问题"><a href="#Git-status中文乱码问题" class="headerlink" title="Git status中文乱码问题"></a>Git status中文乱码问题</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//blog.huzhifeng.com/2016/03/08/Git-Tips-And-Tricks/</span></span><br><span class="line"></span><br><span class="line">git config --global core.quotepath <span class="literal">false</span></span><br><span class="line">root@huzhifeng:~<span class="regexp">/blog# cat ~/</span>.gitconfig</span><br><span class="line">[user]</span><br><span class="line">        name = huzhifeng</span><br><span class="line">        email = zhifeng.hu@email.com</span><br><span class="line">[core]</span><br><span class="line">        editor = vim</span><br><span class="line">        quotepath = <span class="literal">false</span></span><br><span class="line">[alias]</span><br><span class="line">        co = checkout</span><br><span class="line">        st = status</span><br><span class="line">        dt = difftool</span><br><span class="line">        di = diff</span><br><span class="line">root@huzhifeng:~<span class="regexp">/blog#</span></span><br><span class="line">root@huzhifeng:~/blog# git status</span><br><span class="line">On branch master</span><br><span class="line">Your branch is up-to-date <span class="keyword">with</span> <span class="string">'origin/master'</span>.</span><br><span class="line">Untracked files:</span><br><span class="line">  (use <span class="string">"git add &lt;file&gt;..."</span> to include <span class="keyword">in</span> what will be committed)</span><br><span class="line">        source/_posts/WiFi定时开关.md</span><br><span class="line">nothing added to commit but untracked files present (use <span class="string">"git add"</span> to track)</span><br><span class="line">root@huzhifeng:~<span class="regexp">/blog#</span></span><br></pre></td></tr></table></figure>
<h3 id="Cannot-run-program-“svn”"><a href="#Cannot-run-program-“svn”" class="headerlink" title="Cannot run program “svn”"></a>Cannot run program “svn”</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">svn1<span class="number">.8</span>使用了命令工具（command line client tools）默认安装SVN时是未安装此客户端的</span><br><span class="line">安装的时候选择 command line client tools (will be installed on local)</span><br><span class="line">$ ls /d/svn/bin<span class="comment">/*.exe</span></span><br><span class="line"><span class="comment">/d/svn/bin/ConnectVPN.exe*   /d/svn/bin/TortoiseBlame.exe*  /d/svn/bin/TortoiseProc.exe*</span></span><br><span class="line"><span class="comment">/d/svn/bin/sendrpt.exe*      /d/svn/bin/TortoiseIDiff.exe*  /d/svn/bin/TortoiseUDiff.exe*</span></span><br><span class="line"><span class="comment">/d/svn/bin/SubWCRev.exe*     /d/svn/bin/TortoiseMerge.exe*  /d/svn/bin/TSVNCache.exe*</span></span><br><span class="line"><span class="comment">/d/svn/bin/SubWCRevCOM.exe*  /d/svn/bin/TortoisePlink.exe*</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">安装后就有svn 命令 </span></span><br><span class="line"><span class="comment">$ ls /d/svn/bin/*.exe</span></span><br><span class="line"><span class="comment">/d/svn/bin/ConnectVPN.exe*   /d/svn/bin/svndumpfilter.exe*  /d/svn/bin/TortoiseBlame.exe*</span></span><br><span class="line"><span class="comment">/d/svn/bin/sendrpt.exe*      /d/svn/bin/svnlook.exe*        /d/svn/bin/TortoiseIDiff.exe*</span></span><br><span class="line"><span class="comment">/d/svn/bin/SubWCRev.exe*     /d/svn/bin/svnmucc.exe*        /d/svn/bin/TortoiseMerge.exe*</span></span><br><span class="line"><span class="comment">/d/svn/bin/SubWCRevCOM.exe*  /d/svn/bin/svnrdump.exe*       /d/svn/bin/TortoisePlink.exe*</span></span><br><span class="line"><span class="comment">/d/svn/bin/svn.exe*          /d/svn/bin/svnserve.exe*       /d/svn/bin/TortoiseProc.exe*</span></span><br><span class="line"><span class="comment">/d/svn/bin/svnadmin.exe*     /d/svn/bin/svnsync.exe*        /d/svn/bin/TortoiseUDiff.exe*</span></span><br><span class="line"><span class="comment">/d/svn/bin/svnbench.exe*     /d/svn/bin/svnversion.exe*     /d/svn/bin/TSVNCache.exe*</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">https://www.jianshu.com/p/9cd383bdbe09 PhpStorm中如何配置SVN</span></span><br></pre></td></tr></table></figure>
<h3 id="svn比较版本-svn-try-setting-http-chunked-requests-to-auto-or-no"><a href="#svn比较版本-svn-try-setting-http-chunked-requests-to-auto-or-no" class="headerlink" title="svn比较版本 svn try setting http-chunked-requests to auto or no"></a>svn比较版本 svn try setting http-chunked-requests to auto or no</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//stackoverflow.com/questions/17399664/subversion-1-8-0-client-working-against-a-1-6-11-server </span></span><br><span class="line">https:<span class="comment">//www.cnblogs.com/QLeelulu/archive/2009/12/09/1619927.html </span></span><br><span class="line">vi C:\Users\xxx\AppData\Roaming\Subversion\servers</span><br><span class="line">[global]</span><br><span class="line"># http-proxy-exceptions = *.exception.com, www.internal-site.org</span><br><span class="line"># http-proxy-host = defaultproxy.whatever.com</span><br><span class="line"># http-proxy-port = 7000</span><br><span class="line"># http-proxy-username = defaultusername</span><br><span class="line"># http-proxy-password = defaultpassword</span><br><span class="line"># http-compression = auto</span><br><span class="line"># No http-timeout, so just use the builtin default.</span><br><span class="line"># ssl-authority-files = /path/to/CAcert.pem;/path/to/CAcert2.pem</span><br><span class="line">#</span><br><span class="line"># Password / passphrase caching parameters:</span><br><span class="line"># store-passwords = no</span><br><span class="line"># store-ssl-client-cert-pp = no</span><br><span class="line"># store-plaintext-passwords = no</span><br><span class="line"># store-ssl-client-cert-pp-plaintext = no</span><br><span class="line">http-chunked-requests = no</span><br><span class="line">先选择ShowLog查看修改记录，然后选择你要比较的两个版本，然后按右键选 Compare revisions 单机对应文件可以看到差异</span><br><span class="line">比较后选中文件导出差异文件</span><br></pre></td></tr></table></figure>
<h3 id="ipv6惹的祸"><a href="#ipv6惹的祸" class="headerlink" title="ipv6惹的祸"></a>ipv6惹的祸</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Operation timed out after <span class="number">0</span> milliseconds <span class="keyword">with</span> <span class="number">0</span> out <span class="keyword">of</span> <span class="number">0</span> bytes received</span><br><span class="line"> </span><br><span class="line">Resolving timed out after <span class="number">5514</span> milliseconds</span><br><span class="line">wget www.domain.com</span><br><span class="line">-<span class="number">-2016</span><span class="number">-11</span><span class="number">-19</span> <span class="number">22</span>:<span class="number">17</span>:<span class="number">30</span>--  http:<span class="comment">//www.domain.com/</span></span><br><span class="line">Resolving www.domain.com... # 此处停滞约 5 秒</span><br><span class="line">xxx.xxx.xxx.xxx</span><br><span class="line">Connecting to www.domain.com|xxx.xxx.xxx.xxx|:<span class="number">80.</span>.. connected.</span><br><span class="line">HTTP request sent, awaiting response... <span class="number">200</span> OK</span><br><span class="line"></span><br><span class="line">curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );</span><br></pre></td></tr></table></figure>
<h3 id="php-只接受1000个-请求参数"><a href="#php-只接受1000个-请求参数" class="headerlink" title="php 只接受1000个 请求参数"></a>php 只接受1000个 请求参数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">在查看php的error log 发现有句提示如下</span><br><span class="line">http:<span class="comment">//www.54php.cn/default/156.html</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Unknown: Input variables exceeded <span class="number">1000</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">这句提示就是 最多<span class="number">1000</span> 的请求参数，这个在某些项目中<span class="number">1000</span>可能满足不了需求，所以我们需要修改下，找到php.ini文件 ，搜索关键字  max_input_vars  根据项目需求设置满足你需求的值</span><br></pre></td></tr></table></figure>
<h3 id="502-Bad-Gateway错误"><a href="#502-Bad-Gateway错误" class="headerlink" title="502 Bad Gateway错误"></a>502 Bad Gateway错误</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">php程序执行时间过长而超时，检查nginx和fastcgi中各种timeout设置。</span><br><span class="line"></span><br><span class="line">nginx:</span><br><span class="line"></span><br><span class="line">fastcgi_connect_timeout <span class="number">300</span>;</span><br><span class="line"></span><br><span class="line">fastcgi_send_timeout <span class="number">300</span>;</span><br><span class="line"></span><br><span class="line">fastcgi_read_timeout300;</span><br><span class="line"></span><br><span class="line">keepalive_timeout;</span><br><span class="line"></span><br><span class="line">php-fpm:</span><br><span class="line"></span><br><span class="line">request_terminate_timeout=<span class="number">300</span>;</span><br><span class="line"></span><br><span class="line">php.ini:</span><br><span class="line"></span><br><span class="line">max_execution_time=<span class="number">300</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">不过request_terminate_timeout参数会直接杀掉php进程，然后重启php进程，这样前端nginx就会返回<span class="number">104</span>: Connection reset by peer，最好设成request_terminate_timeout=<span class="number">0</span>;但最重要的是程序里要设置好超时，不要使用php-fpm的request_terminate_timeout。https:<span class="comment">//blog.51cto.com/smileyouth/1586237</span></span><br><span class="line">使用 netstat -napo |grep <span class="string">"php-fpm"</span> | wc -l 查看一下当前fastcgi进程个数，如果个数接近conf里配置的上限，就需要调高进程数。</span><br><span class="line">http:<span class="comment">//www.nginx.cn/102.html</span></span><br><span class="line">[root@localhost ~]# grep slow  /etc/php-fpm.d/www.conf</span><br><span class="line">; - <span class="string">'slowlog'</span></span><br><span class="line">; The log file <span class="keyword">for</span> slow requests</span><br><span class="line">; Note: slowlog is mandatory <span class="keyword">if</span> request_slowlog_timeout is set</span><br><span class="line">slowlog = <span class="regexp">/var/</span>log/php7/slow.log</span><br><span class="line">; dumped to the <span class="string">'slowlog'</span> file. A value <span class="keyword">of</span> <span class="string">'0s'</span> means <span class="string">'off'</span>.</span><br><span class="line">request_slowlog_timeout = <span class="number">2</span>s</span><br><span class="line"></span><br><span class="line">[root@localhost ~]# tail -f /var/log/php7/slow.log</span><br><span class="line">[<span class="number">0x00007f347c6143a0</span>] xhprof_generate_image_by_dot() /data1/projects/public/xhprof/xhprof_lib/utils/callgraph_utils.php:<span class="number">450</span></span><br><span class="line">[<span class="number">0x00007f347c614270</span>] xhprof_get_content_by_run() /data1/projects/public/xhprof/xhprof_lib/utils/callgraph_utils.php:<span class="number">474</span></span><br><span class="line">[<span class="number">0x00007f347c614170</span>] xhprof_render_image() /data1/projects/public/xhprof/xhprof_html/callgraph.php:<span class="number">85</span></span><br><span class="line"></span><br><span class="line">[<span class="number">22</span>-Feb<span class="number">-2019</span> <span class="number">17</span>:<span class="number">20</span>:<span class="number">07</span>]  [pool www] pid <span class="number">3899</span></span><br><span class="line">script_filename = <span class="regexp">/data1/</span>projects/public/xhprof/xhprof_html/callgraph.php</span><br><span class="line">[<span class="number">0x00007f347c6144c0</span>] stream_get_contents() /data1/projects/public/xhprof/xhprof_lib/utils/callgraph_utils.php:<span class="number">117</span></span><br><span class="line">[<span class="number">0x00007f347c6143a0</span>] xhprof_generate_image_by_dot() /data1/projects/public/xhprof/xhprof_lib/utils/callgraph_utils.php:<span class="number">450</span></span><br><span class="line">[<span class="number">0x00007f347c614270</span>] xhprof_get_content_by_run() /data1/projects/public/xhprof/xhprof_lib/utils/callgraph_utils.php:<span class="number">474</span></span><br><span class="line">[<span class="number">0x00007f347c614170</span>] xhprof_render_image() /data1/projects/public/xhprof/xhprof_html/callgraph.php:<span class="number">85</span></span><br><span class="line"></span><br><span class="line">[root@localhost ~]# cat /etc/php-fpm.conf|grep error</span><br><span class="line">;error_log = log/php-fpm.log</span><br><span class="line">error_log = <span class="regexp">/var/</span>log/php-fpm.log</span><br><span class="line"></span><br><span class="line">tail -f /<span class="keyword">var</span>/log/php-fpm.log</span><br><span class="line">[<span class="number">22</span>-Feb<span class="number">-2019</span> <span class="number">16</span>:<span class="number">53</span>:<span class="number">21</span>] WARNING: [pool www] child <span class="number">1649</span>, script <span class="string">'/data1/projects/suping/public/xhprof/xhprof_html/callgraph.php'</span> (request: <span class="string">"GET /public/xhprof/xhprof_html/callgraph.php"</span>) executing too slow (<span class="number">2.619120</span> sec), logging</span><br><span class="line">[<span class="number">22</span>-Feb<span class="number">-2019</span> <span class="number">16</span>:<span class="number">53</span>:<span class="number">21</span>] NOTICE: child <span class="number">1649</span> stopped <span class="keyword">for</span> tracing</span><br><span class="line">[<span class="number">22</span>-Feb<span class="number">-2019</span> <span class="number">16</span>:<span class="number">53</span>:<span class="number">21</span>] NOTICE: about to trace <span class="number">1649</span></span><br><span class="line">[<span class="number">22</span>-Feb<span class="number">-2019</span> <span class="number">16</span>:<span class="number">53</span>:<span class="number">21</span>] NOTICE: finished trace <span class="keyword">of</span> <span class="number">1649</span></span><br><span class="line">[<span class="number">22</span>-Feb<span class="number">-2019</span> <span class="number">16</span>:<span class="number">53</span>:<span class="number">29</span>] WARNING: [pool www] child <span class="number">1649</span>, script <span class="string">'/data1/projects/suping/public/xhprof/xhprof_html/callgraph.php'</span> (request: <span class="string">"GET /public/xhprof/xhprof_html/callgraph.php"</span>) execution timed out (<span class="number">10.619676</span> sec), terminating</span><br><span class="line">[<span class="number">22</span>-Feb<span class="number">-2019</span> <span class="number">16</span>:<span class="number">53</span>:<span class="number">29</span>] WARNING: [pool www] child <span class="number">1649</span> exited on signal <span class="number">15</span> (SIGTERM) after <span class="number">234.714340</span> seconds <span class="keyword">from</span> start</span><br><span class="line">[<span class="number">22</span>-Feb<span class="number">-2019</span> <span class="number">16</span>:<span class="number">53</span>:<span class="number">29</span>] NOTICE: [pool www] child <span class="number">2342</span> started</span><br><span class="line">[root@localhost suping]# grep timeout /data0/opt/php7/etc/php-fpm.d/www.conf</span><br><span class="line">;             pm.process_idle_timeout   - The number <span class="keyword">of</span> seconds after which</span><br><span class="line">;pm.process_idle_timeout = <span class="number">10</span>s;</span><br><span class="line">; Note: slowlog is mandatory <span class="keyword">if</span> request_slowlog_timeout is set</span><br><span class="line">; The timeout <span class="keyword">for</span> serving a single request after which a PHP backtrace will be</span><br><span class="line">request_slowlog_timeout = <span class="number">2</span>s</span><br><span class="line">; The timeout <span class="keyword">for</span> serving a single request after which the worker process will</span><br><span class="line">request_terminate_timeout = <span class="number">10</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">内存过高，php脚本执行超时这两种情况都返回的是<span class="number">500.</span></span><br><span class="line"><span class="number">500</span>和<span class="number">502</span>的区别是，</span><br><span class="line"><span class="number">500</span>是php脚本自身错误（内存超出配置memory_limit；超过cpu执行时间set_time_limit(),语法错误等）</span><br><span class="line"><span class="number">502</span>是php-fpm进程本身挂掉（php-fpm未启动；request_terminate_timeout超时）</span><br><span class="line"></span><br><span class="line">php-fpm进程和php脚本是两回事，因此状态码上也会有区别。</span><br><span class="line">php-cgi进程数不够用、php执行时间长、或者是php-cgi进程死掉，都会出现<span class="number">502</span>错误。慢查询<span class="number">2</span>s，超时<span class="number">10</span>s，改为<span class="number">15</span></span><br></pre></td></tr></table></figure>
<h3 id="字符集转转换失败"><a href="#字符集转转换失败" class="headerlink" title="字符集转转换失败"></a>字符集转转换失败</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$xml = iconv(<span class="string">'UTF-8'</span>, <span class="string">'GBK'</span>, $xml);</span><br><span class="line">就是这一步，iconv转换失败的时候会返回<span class="literal">false</span>而不是报错，也没有日志记录，所以 $xml 被置为 <span class="literal">false</span> 了。。。。。至于为什么会失败，我也不知道，应该是有某种特殊编码把，有一个办法可以跳过这个转换失败,就是在目标字符集加一个选项：</span><br><span class="line"></span><br><span class="line">$xml = iconv(<span class="string">'UTF-8'</span>, <span class="string">'GBK//IGNORE'</span>, $xml);</span><br><span class="line">http:<span class="comment">//blog.wuxu92.com/php-iconv-ignore-option/</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP7-MongoDB"><a href="#PHP7-MongoDB" class="headerlink" title="PHP7 MongoDB"></a>PHP7 MongoDB</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">$mongo = <span class="keyword">new</span> MongoDB\Driver\Manager();</span><br><span class="line">$cursor = $mongo-&gt;executeQuery(<span class="string">'db.collection'</span>, <span class="keyword">new</span> MongoDB\Driver\Query($arr, $opts), <span class="keyword">new</span> MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED));</span><br><span class="line"><span class="comment">//注意，这里的 $arr和 $opts; https://my.oschina.net/jsk/blog/644287</span></span><br><span class="line">$arr = [<span class="string">'_id'</span>=&gt; [<span class="string">'$in'</span>=&gt; $ids] ]; <span class="comment">// 根据id数组获取集合</span></span><br><span class="line">$opts = [<span class="string">'limit'</span>=&gt; $limit, <span class="string">'skip'</span>=&gt; $skip ]; <span class="comment">// 不推荐</span></span><br><span class="line"><span class="comment">// 这里在传递$limit和$skip的时候，最好先转成整数，不然不生效：</span></span><br><span class="line">$opts = [<span class="string">'limit'</span>=&gt; (int) $limit, <span class="string">'skip'</span>=&gt; (int) $skip ]; <span class="comment">// 推荐写法。</span></span><br><span class="line"><span class="comment">// 写到这里，让人怀疑是不是在用php了，怎么还需要这样的类型转换。字符串整数都不行！</span></span><br></pre></td></tr></table></figure>
<h3 id="MISCONF-Redis-is-configured-to-save-RDB-snapshots-1"><a href="#MISCONF-Redis-is-configured-to-save-RDB-snapshots-1" class="headerlink" title="MISCONF Redis is configured to save RDB snapshots"></a>MISCONF Redis is configured to save RDB snapshots</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"> Redis 被配置为保存 RDB 快照，但是现在无法写入磁盘，那么这无非是两个问题，一个是没有权限，一个是空间不足，于是立即登录到 Redis 所在服务器，分别查看了权限和空间都没有问题，当务之急是先修复问题，然后再定位原因，所以立即在服务启动 redis-cli，将 stop-writes-on-bgsave-error 设置为 on（该选项在 Redis 配置文件中默认配置为 yes，表示 Redis 报错时停止写入并报错，我们将其设置为 on 表示忽略报错，继续执行）：</span><br><span class="line">https:<span class="comment">//laravelacademy.org/post/9666.html</span></span><br><span class="line">#redis-cli </span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; config set stop-writes-on-bgsave-error no</span><br><span class="line">打开 Redis Server 的日志文件 /<span class="keyword">var</span>/log/redis/redis-server.log，想从这里寻找蛛丝马迹，果不其然，发现了大量报错日志：</span><br><span class="line"></span><br><span class="line">14632:C 09 Nov 08:58:14.020 # Failed opening .rdb for saving: Read-only file system</span><br><span class="line">30211:M 09 Nov 08:58:14.120 # Background saving error</span><br><span class="line"></span><br><span class="line">把 Redis 配置文件中 bind 选项配置为了 <span class="number">0.0</span><span class="number">.0</span><span class="number">.0</span>，以便让多个机器可以访问，但是没有配置任何权限和 iptables 过滤规则，这样导致的结果是外网可以自由访问我的 Redis 服务器，这种故障会导致 Redis 被 block</span><br><span class="line">配置允许 <span class="number">192.168</span><span class="number">.0</span><span class="number">.1</span> 和 <span class="number">192.168</span><span class="number">.0</span><span class="number">.2</span> 这两个 IP 访问 <span class="number">6379</span> 端口（具体 IP 以你自己服务器的公网 IP 为准，允许自身访问 IP 可以配置为 <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>）：</span><br><span class="line"></span><br><span class="line"># 禁止任何 IP 访问 6379 端口</span><br><span class="line">iptables -I INPUT -p TCP --dport <span class="number">6379</span> -j REJECT</span><br><span class="line"># 允许指定 IP 访问 6379 端口</span><br><span class="line">iptables -I INPUT -s <span class="number">192.168</span><span class="number">.0</span><span class="number">.1</span> -p tcp --dport <span class="number">6379</span> -j ACCEPT</span><br><span class="line">iptables -I INPUT -s <span class="number">192.168</span><span class="number">.0</span><span class="number">.2</span> -p tcp --dport <span class="number">6379</span> -j ACCEPT</span><br><span class="line"># 保存并生效</span><br><span class="line">iptables-save</span><br><span class="line">然后我们需要通过 kill <span class="number">-9</span> 杀死 redis-server 进程，并通过 redis-server /etc/redis/redis.conf 重新启动 </span><br><span class="line">如果你是将 Redis 服务器安装在应用所在服务器，并且只有这么一台机器，不用这么大费周章的配置，只需在 Redis 配置文件中将 bind 选项设置为 <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> 只允许本机访问即可。</span><br></pre></td></tr></table></figure>
<h3 id="mongodb3-6-The-‘cursor’-option-is-required-except-for-aggregate-with-the-explain-argument"><a href="#mongodb3-6-The-‘cursor’-option-is-required-except-for-aggregate-with-the-explain-argument" class="headerlink" title="mongodb3.6 The ‘cursor’ option is required, except for aggregate with the explain argument"></a>mongodb3.6 The ‘cursor’ option is required, except for aggregate with the explain argument</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Mongo-记一次安装启动异常 http://zhangyuyu.github.io/2017/12/27/Mongo-%E8%AE%B0%E4%B8%80%E6%AC%A1%E5%AE%89%E8%A3%85%E5%90%AF%E5%8A%A8%E9%94%99%E8%AF%AF/</span></span><br><span class="line"><span class="comment">//https://github.com/jenssegers/laravel-mongodb/pull/1570/commits/6076df9cee559ff6e75956b7a0f9ccf7c35f76b5#diff-c19a83820d10bafa13d449ec36d0b360</span></span><br><span class="line"><span class="comment">//https://github.com/jenssegers/laravel-mongodb/pull/1569/commits/36222977ca52635b7d06377f9553fe67b903fede#diff-d013b1af075671441bada29dbedab6ff</span></span><br><span class="line">/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Collection.php:<span class="number">47</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">__call</span>(<span class="params">$method, $parameters</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $start = microtime(<span class="literal">true</span>);</span><br><span class="line">        <span class="comment">//增加判断</span></span><br><span class="line">        <span class="keyword">if</span>($method == <span class="string">'aggregate'</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            $parameters[] = [<span class="string">'cursor'</span> =&gt;(object) []];</span><br><span class="line">        &#125;</span><br><span class="line">        $result = call_user_func_array([$<span class="keyword">this</span>-&gt;collection, $method], $parameters);</span><br><span class="line">        <span class="keyword">if</span>($method == <span class="string">'aggregate'</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            $result[<span class="string">'result'</span>] = $result[<span class="string">'cursor'</span>][<span class="string">'firstBatch'</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;connection-&gt;logging())</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// Once we have run the query we will calculate the time that it took to run and</span></span><br><span class="line">            <span class="comment">// then log the query, bindings, and execution time so we will report them on</span></span><br><span class="line">            <span class="comment">// the event that the developer needs them. We'll log time in milliseconds.</span></span><br><span class="line">            $time = $<span class="keyword">this</span>-&gt;connection-&gt;getElapsedTime($start);</span><br><span class="line"></span><br><span class="line">            $query = [];</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Convert the query paramters to a json string.</span></span><br><span class="line">            foreach ($parameters <span class="keyword">as</span> $parameter)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">try</span></span><br><span class="line">                &#123;</span><br><span class="line">                    $query[] = json_encode($parameter);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">catch</span> (Exception $e)</span><br><span class="line">                &#123;</span><br><span class="line">                    $query[] = <span class="string">'&#123;...&#125;'</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            $queryString = $<span class="keyword">this</span>-&gt;collection-&gt;getName() . <span class="string">'.'</span> . $method . <span class="string">'('</span> . implode(<span class="string">','</span>, $query) . <span class="string">')'</span>;</span><br><span class="line"></span><br><span class="line">            $<span class="keyword">this</span>-&gt;connection-&gt;logQuery($queryString, [], $time);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// /vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Query/Builder.php:228</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">getFresh</span>(<span class="params">$columns = []</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// If no columns have been specified for the select statement, we will set them</span></span><br><span class="line">        <span class="comment">// here to either the passed columns, or the standard default of retrieving</span></span><br><span class="line">        <span class="comment">// all of the columns on the table using the "wildcard" column character.</span></span><br><span class="line">        <span class="keyword">if</span> (is_null($<span class="keyword">this</span>-&gt;columns)) $<span class="keyword">this</span>-&gt;columns = $columns;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Drop all columns if * is present, MongoDB does not work this way.</span></span><br><span class="line">        <span class="keyword">if</span> (in_array(<span class="string">'*'</span>, $<span class="keyword">this</span>-&gt;columns)) $<span class="keyword">this</span>-&gt;columns = [];</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Compile wheres</span></span><br><span class="line">        $wheres = $<span class="keyword">this</span>-&gt;compileWheres();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Use MongoDB's aggregation framework when using grouping or aggregation functions.</span></span><br><span class="line">        <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;groups or $<span class="keyword">this</span>-&gt;aggregate or $<span class="keyword">this</span>-&gt;paginating)</span><br><span class="line">        &#123;</span><br><span class="line">            $group = [];</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Add grouping columns to the $group part of the aggregation pipeline.</span></span><br><span class="line">            <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;groups)</span><br><span class="line">            &#123;</span><br><span class="line">                foreach ($<span class="keyword">this</span>-&gt;groups <span class="keyword">as</span> $column)</span><br><span class="line">                &#123;</span><br><span class="line">                    $group[<span class="string">'_id'</span>][$column] = <span class="string">'$'</span> . $column;</span><br><span class="line"></span><br><span class="line">                    <span class="comment">// When grouping, also add the $last operator to each grouped field,</span></span><br><span class="line">                    <span class="comment">// this mimics MySQL's behaviour a bit.</span></span><br><span class="line">                    $group[$column] = [<span class="string">'$last'</span> =&gt; <span class="string">'$'</span> . $column];</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                <span class="comment">// Do the same for other columns that are selected.</span></span><br><span class="line">                foreach ($<span class="keyword">this</span>-&gt;columns <span class="keyword">as</span> $column)</span><br><span class="line">                &#123;</span><br><span class="line">                    $key = str_replace(<span class="string">'.'</span>, <span class="string">'_'</span>, $column);</span><br><span class="line"></span><br><span class="line">                    $group[$key] = [<span class="string">'$last'</span> =&gt; <span class="string">'$'</span> . $column];</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Add aggregation functions to the $group part of the aggregation pipeline,</span></span><br><span class="line">            <span class="comment">// these may override previous aggregations.</span></span><br><span class="line">            <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;aggregate)</span><br><span class="line">            &#123;</span><br><span class="line">                $function = $this-&gt;aggregate['function'];</span><br><span class="line"></span><br><span class="line">                foreach ($<span class="keyword">this</span>-&gt;aggregate[<span class="string">'columns'</span>] <span class="keyword">as</span> $column)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="comment">// Translate count into sum.</span></span><br><span class="line">                    <span class="keyword">if</span> ($<span class="function"><span class="keyword">function</span> == '<span class="title">count</span>')</span></span><br><span class="line"><span class="function">                    </span>&#123;</span><br><span class="line">                        $group[<span class="string">'aggregate'</span>] = [<span class="string">'$sum'</span> =&gt; <span class="number">1</span>];</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="comment">// Pass other functions directly.</span></span><br><span class="line">                    <span class="keyword">else</span></span><br><span class="line">                    &#123;</span><br><span class="line">                        $group[<span class="string">'aggregate'</span>] = [<span class="string">'$'</span> . $<span class="function"><span class="keyword">function</span> =&gt; '<span class="title">$</span>' . <span class="title">$column</span>];</span></span><br><span class="line"><span class="function">                    &#125;</span></span><br><span class="line"><span class="function">                &#125;</span></span><br><span class="line"><span class="function">            &#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">            // <span class="title">When</span> <span class="title">using</span> <span class="title">pagination</span>, <span class="title">we</span> <span class="title">limit</span> <span class="title">the</span> <span class="title">number</span> <span class="title">of</span> <span class="title">returned</span> <span class="title">columns</span></span></span><br><span class="line"><span class="function">            // <span class="title">by</span> <span class="title">adding</span> <span class="title">a</span> <span class="title">projection</span>.</span></span><br><span class="line"><span class="function">            <span class="title">if</span> (<span class="params">$this-&gt;paginating</span>)</span></span><br><span class="line"><span class="function">            </span>&#123;</span><br><span class="line">                foreach ($<span class="keyword">this</span>-&gt;columns <span class="keyword">as</span> $column)</span><br><span class="line">                &#123;</span><br><span class="line">                    $<span class="keyword">this</span>-&gt;projections[$column] = <span class="number">1</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// The _id field is mandatory when using grouping.</span></span><br><span class="line">            <span class="keyword">if</span> ($group and empty($group[<span class="string">'_id'</span>]))</span><br><span class="line">            &#123;</span><br><span class="line">                $group[<span class="string">'_id'</span>] = <span class="literal">null</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Build the aggregation pipeline.</span></span><br><span class="line">            $pipeline = [];</span><br><span class="line">            <span class="keyword">if</span> ($wheres) $pipeline[] = [<span class="string">'$match'</span> =&gt; $wheres];</span><br><span class="line">            <span class="keyword">if</span> ($group)  $pipeline[] = [<span class="string">'$group'</span> =&gt; $group];</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Apply order and limit</span></span><br><span class="line">            <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;orders)      $pipeline[] = [<span class="string">'$sort'</span> =&gt; $<span class="keyword">this</span>-&gt;orders];</span><br><span class="line">            <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;offset)      $pipeline[] = [<span class="string">'$skip'</span> =&gt; $<span class="keyword">this</span>-&gt;offset];</span><br><span class="line">            <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;limit)       $pipeline[] = [<span class="string">'$limit'</span> =&gt; $<span class="keyword">this</span>-&gt;limit];</span><br><span class="line">            <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;projections) $pipeline[] = [<span class="string">'$project'</span> =&gt; $<span class="keyword">this</span>-&gt;projections];</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Execute aggregation</span></span><br><span class="line">            $results = $<span class="keyword">this</span>-&gt;collection-&gt;aggregate($pipeline);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Return results 更改返回值</span></span><br><span class="line">            <span class="keyword">return</span> $results[<span class="string">'cursor'</span>][<span class="string">'firstBatch'</span>];</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
<h3 id="php发送email-多了"><a href="#php发送email-多了" class="headerlink" title="php发送email 多了!"></a>php发送email 多了!</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//blog.csdn.net/yylad/article/details/9048871</span></span><br><span class="line"></span><br><span class="line">造成这种现象的两种可能和解决办法:</span><br><span class="line"></span><br><span class="line"> html表情和CSS与邮件客户端不兼容, </span><br><span class="line">以outlook为例, 参考http:<span class="comment">//msdn.microsoft.com/zh-cn/library/aa338201(v=office.12).aspx, 这个问题没其他办法, 只能自己优化代码, 采用标准化写法, 如标签尽量完整, 浏览器可以识别&lt;br&gt;表情, 但要换成标准写法&lt;br/&gt;, 还有就是记得闭合标签.</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">邮件内容过长, 导致解析出问题.</span><br><span class="line">通常邮件信息每行不得超过<span class="number">998</span>个字符, 而整个html邮件体中没有换行符, 会自动的被认为是一行, 所以就会有问题 (<span class="number">998</span> 听起来有点像电视购物呢 - -!!). 怎么办呢?</span><br><span class="line"></span><br><span class="line">两种解决办法:</span><br><span class="line"></span><br><span class="line"><span class="number">1.</span> 人工增加换行符: 以上面代码为例, 将$body取值替换为 </span><br><span class="line"></span><br><span class="line">$body = wordwrap(get_email_body($an_array),<span class="number">900</span>, <span class="string">"\n"</span>);</span><br><span class="line">即, 在邮件体字符串中,每<span class="number">900</span>个字符加一个换行符, 这样就保证了每行字符串不会超过上限. 但同时也会带来新的问题, 因为额外加的字符串在某些情况下, 可能会导致邮件布局有问题(特定浏览器, 操作系统或者邮件客户端).</span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 用base64编码 </span><br><span class="line"></span><br><span class="line">$header = <span class="string">"MIME-Version: 1.0\r\nContent-Transfer-Encoding: base64\r\nContent-type: text/html; charset=utf-8\r\nFrom: test&lt;test@test.com&gt;"</span>;</span><br><span class="line">$body = rtrim(chunk_split(base64_encode(get_email_body($an_array))));</span><br><span class="line">首先给body添加base64编码, 然后在header中添加Content-Transfer-Encoding: base64说明. 因为base64编码会自动每<span class="number">76</span>个字符添加一个换行符, 所以自然满足了每<span class="number">998</span>字符换行的问题.</span><br><span class="line">--------------------- </span><br><span class="line"> </span><br><span class="line">If you are seeing unwanted line breaks preceded by exclamation marks (<span class="string">"!"</span>) <span class="keyword">in</span> the emails you send <span class="keyword">with</span> mail(), you may be having the same problem I had: exceeding the maximum line length (<span class="number">998</span> chars) specified <span class="keyword">in</span> RFC <span class="number">2822</span> (http:<span class="comment">//www.faqs.org/rfcs/rfc2822.html).</span></span><br><span class="line">You can get around <span class="keyword">this</span> restriction by using base64 encoding: add a <span class="string">"Content-Transfer-Encoding: base64"</span> header and encode the contents <span class="keyword">with</span></span><br><span class="line">$base64contents = rtrim(chunk_split(</span><br><span class="line">base64_encode($contents)));</span><br><span class="line"></span><br><span class="line"> $headers = <span class="string">"From:someone@somewhere.com\r\n"</span>;</span><br><span class="line">  $headers .= <span class="string">"MIME-Version: 1.0\r\n"</span>;</span><br><span class="line">  $headers .= <span class="string">"Content-Type: text/html; charset=utf-8\r\n"</span>;</span><br><span class="line">  $headers .= <span class="string">"Content-Transfer-Encoding:base64 \r\n"</span>;</span><br><span class="line">  $message = <span class="string">"THIS IS A TEST"</span>;</span><br><span class="line">  $messagebody= $base64contents = rtrim(chunk_split(base64_encode($message)));</span><br><span class="line">    mail(tofoo@bar.com, $subject, $messagebody, $headers )</span><br><span class="line">    </span><br><span class="line">    @mail($to, $subject, $msg, <span class="string">"From: $from\nContent-Type: text/html; charset=iso-8859-1; Content-Transfer-Encoding: base64"</span>);</span><br></pre></td></tr></table></figure>
<h3 id="PhpSpreadsheet-输出-Excel-时自动在-xls-后面加入-html"><a href="#PhpSpreadsheet-输出-Excel-时自动在-xls-后面加入-html" class="headerlink" title="PhpSpreadsheet 输出 Excel 时自动在 xls 后面加入 html"></a>PhpSpreadsheet 输出 Excel 时自动在 xls 后面加入 html</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//http://www.sunbelife.com/p/b63c.html</span></span><br><span class="line">header(<span class="string">'Content-Type: application/vnd.ms-excel'</span>);</span><br><span class="line">header(<span class="string">'Content-Disposition: attachment;filename="赴宴信息.xls"'</span>);</span><br><span class="line">header(<span class="string">'Cache-Control: max-age=0'</span>);</span><br><span class="line"></span><br><span class="line">$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, <span class="string">'Xls'</span>);</span><br><span class="line">$writer-&gt;save(<span class="string">'php://output'</span>);</span><br><span class="line">exit;</span><br></pre></td></tr></table></figure>
<h3 id="Composer-使用镜像出现权限问题"><a href="#Composer-使用镜像出现权限问题" class="headerlink" title="Composer 使用镜像出现权限问题"></a>Composer 使用镜像出现权限问题</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">vagrant@homestead:~<span class="regexp">/Code/</span>larabbs$ composer <span class="built_in">require</span> <span class="string">"overtrue/laravel-lang:~3.0"</span></span><br><span class="line">./composer.json has been updated</span><br><span class="line">Loading composer repositories <span class="keyword">with</span> package information</span><br><span class="line">Updating dependencies (including <span class="built_in">require</span>-dev)</span><br><span class="line">file_put_contents(<span class="regexp">/home/</span>vagrant/.composer/cache/repo/https---packagist.laravel-china.org/provider-illuminate$<span class="built_in">console</span>.json): failed to open stream: Permission denied</span><br><span class="line">https:<span class="comment">//packagist.laravel-china.org could not be fully loaded, package information was loaded from the local cache and may be out of date</span></span><br><span class="line"></span><br><span class="line">sudo chown -R $USER ~<span class="regexp">/.composer/</span> </span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/29560</span></span><br></pre></td></tr></table></figure>
<h3 id="No-query-results-for-model"><a href="#No-query-results-for-model" class="headerlink" title="No query results for model"></a>No query results for model</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">如果将</span><br><span class="line"></span><br><span class="line">Route::get(<span class="string">'/users/&#123;user&#125;'</span>, <span class="string">'UserController@show'</span>)-&gt;name(<span class="string">'users.show'</span>);</span><br><span class="line">放在了</span><br><span class="line"></span><br><span class="line">Route::get(<span class="string">'/users/info'</span>, <span class="string">'UserController@info'</span>)-&gt;name(<span class="string">'users.info'</span>);</span><br><span class="line">前面的话，请求 /users/info 会提示</span><br><span class="line"></span><br><span class="line">No query results <span class="keyword">for</span> model [App\Post].</span><br><span class="line">原来测试的时候没有想过路由冲突的问题。通过更换路由先后顺序确实是可以解决这个情况。</span><br><span class="line">但是需要这样解决，太无法体现 laravel 的优雅了。我们可以用上路由提供的正则表达式来很好解决这个问题。</span><br><span class="line"></span><br><span class="line"><span class="comment">//用户信息</span></span><br><span class="line">Route::get(<span class="string">'/users/&#123;user&#125;'</span>, <span class="string">'UserController@show'</span>)-&gt;where(<span class="string">'user'</span>, <span class="string">'[0-9]+'</span>)-&gt;name(<span class="string">'users.show'</span>);</span><br><span class="line">https:<span class="comment">//learnku.com/articles/25947#replies</span></span><br></pre></td></tr></table></figure>
<h3 id="composer-Out-of-memory"><a href="#composer-Out-of-memory" class="headerlink" title="composer Out of memory"></a>composer Out of memory</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">$ composer <span class="built_in">require</span> chenhua/laravel5-markdown-editor</span><br><span class="line">    <span class="number">1</span>/<span class="number">2</span>:        https:<span class="comment">//packagist.laravel-china.org/p/provider-latest$5f7ff05acde3b89b16151c86e800d5ac9afebd3a1b827d539c5f341b3355db2f.json</span></span><br><span class="line">    <span class="number">2</span>/<span class="number">2</span>:        https:<span class="comment">//packagist.laravel-china.org/p/provider-2019-04$ceaf82cd34c625192605362e4eb8f1477899cb63e2a0b3cb527e65d124b42a24.json</span></span><br><span class="line">    Finished: success: <span class="number">2</span>, <span class="attr">skipped</span>: <span class="number">0</span>, <span class="attr">failure</span>: <span class="number">0</span>, <span class="attr">total</span>: <span class="number">2</span></span><br><span class="line">Using version ^<span class="number">1.0</span> <span class="keyword">for</span> chenhua/laravel5-markdown-editor</span><br><span class="line">./composer.json has been updated</span><br><span class="line">Loading composer repositories <span class="keyword">with</span> package information</span><br><span class="line">Updating dependencies (including <span class="built_in">require</span>-dev)</span><br><span class="line"></span><br><span class="line">VirtualAlloc() failed: [<span class="number">0x00000008</span>] ▒洢▒ռ䲻▒㣬▒޷▒▒▒▒▒▒▒▒▒</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">VirtualFree() failed: [<span class="number">0x000001e7</span>] ▒▒ͼ▒▒▒▒▒▒Ч▒ĵ▒ַ▒▒</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">VirtualAlloc() failed: [<span class="number">0x00000008</span>] ▒洢▒ռ䲻▒㣬▒޷▒▒▒▒▒▒▒▒▒</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">VirtualFree() failed: [<span class="number">0x000001e7</span>] ▒▒ͼ▒▒▒▒▒▒Ч▒ĵ▒ַ▒▒</span><br><span class="line"></span><br><span class="line">PHP Fatal error:  Out <span class="keyword">of</span> memory (allocated <span class="number">962592768</span>) (tried to allocate <span class="number">4096</span> bytes) <span class="keyword">in</span> phar:<span class="comment">//D:/php_study/PHPTutorial/php/php-7.1.13-nts/composer/src/Composer/DependencyResolver/RuleWatchGraph.php on line 52</span></span><br><span class="line"></span><br><span class="line">Fatal error: Out <span class="keyword">of</span> memory (allocated <span class="number">962592768</span>) (tried to allocate <span class="number">4096</span> bytes) <span class="keyword">in</span> phar:<span class="comment">//D:/php_study/PHPTutorial/php/php-7.1.13-nts/composer/src/Composer/DependencyResolver/RuleWatchGraph.php on line 52</span></span><br><span class="line"> php -d memory_limit=<span class="number">-1</span> <span class="string">`which composer`</span> update/install -vvv， 去掉内存限制</span><br><span class="line">php -d memory_limit=<span class="number">1</span>G composer <span class="built_in">require</span> chenhua/laravel5-markdown-editor</span><br><span class="line"></span><br><span class="line">sudo vim /etc/php5/cli/php.ini 按: 进入命令行模式 输入 /memory_limit，找到 memory_limit 修改配置为 memory_limit = <span class="number">1024</span>M</span><br><span class="line"></span><br><span class="line">free -m</span><br><span class="line">              total        used        free      shared  buff/cache   available</span><br><span class="line">Mem:            <span class="number">864</span>         <span class="number">372</span>         <span class="number">306</span>          <span class="number">50</span>         <span class="number">185</span>         <span class="number">296</span></span><br><span class="line">Swap:             <span class="number">0</span>           <span class="number">0</span>           <span class="number">0</span></span><br><span class="line"># 如上发现Swap实际都为0</span><br><span class="line">/bin/dd <span class="keyword">if</span>=<span class="regexp">/dev/</span>zero <span class="keyword">of</span>=<span class="regexp">/var/</span>swap<span class="number">.1</span> bs=<span class="number">1</span>M count=<span class="number">1024</span></span><br><span class="line">/sbin/mkswap /<span class="keyword">var</span>/swap<span class="number">.1</span></span><br><span class="line">/sbin/swapon /<span class="keyword">var</span>/swap<span class="number">.1</span></span><br><span class="line"># 再次使用free -m，发现已经有了Swap内存配置</span><br><span class="line">              total        used        free      shared  buff/cache   available</span><br><span class="line">Mem:            <span class="number">864</span>         <span class="number">383</span>          <span class="number">67</span>          <span class="number">49</span>         <span class="number">413</span>         <span class="number">267</span></span><br><span class="line">Swap:          <span class="number">1023</span>           <span class="number">0</span>        <span class="number">1023</span></span><br><span class="line"># 再次运行composer install即可</span><br></pre></td></tr></table></figure>
<h3 id="mysql-慢查询-502"><a href="#mysql-慢查询-502" class="headerlink" title="mysql 慢查询 502"></a>mysql 慢查询 502</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">大量请求响应为<span class="number">502</span>，但每次故障发生时，错误响应一般集中在一台Web服务器</span><br><span class="line">MySQL数据库服务器CPU使用率飙升</span><br><span class="line"></span><br><span class="line">故障刚开始出现时，重启/关闭出现故障现象的MySQL服务，或将出现故障的Web服务器上所有PHP-FPM重启，也能解一时的问题，但治不了本，故障还是频繁出现。</span><br><span class="line"></span><br><span class="line">在故障发生时，从相关服务器上收集到的信息如下所示：</span><br><span class="line"></span><br><span class="line"><span class="number">1.</span>出现故障现象的Web服务器 - CPU使用率、内存使用率等系统指标均正常，但PHP-FPM子进程数达到上限（<span class="number">12</span> x <span class="number">10</span> = <span class="number">120</span>），并且PHP-FPM进程与数据库代理服务器之间的网络连接数量较多（与PHP-FPM子进程数大致相当）</span><br><span class="line"></span><br><span class="line"><span class="number">2.</span>出现故障现象的MySQL服务器 - CPU使用率飙升，主要为MySQL进程占用；MySQL进程与数据库代理服务器之间的网络连接较多</span><br><span class="line"></span><br><span class="line"><span class="number">3.</span>继而，对出现故障现象的MySQL服务器上的数据库执行命令SHOW PROCESSLIST（查看当前MySQL实例运行着哪些线程）</span><br><span class="line"></span><br><span class="line">Query：表明当前线程正在执行一次SQL查询操作。该SQL为SELECT h.host, p.result, p.update_time FROM PIXIU p join Host h using(host_id) WHERE ...，线程所处状态为“Sorting result”（正在创建排序索引），持续时间为<span class="number">86</span><span class="number">-99</span>秒左右。很明显，这句SQL语句花费的时间过长，存在问题。</span><br><span class="line">综合上面所述，可以引出一个猜测：由于这条SQL查询需耗费较长时间，并且被频繁执行，涉及该SQL的请求需要较长时间完成，大量SQL线程排队无响应，阻塞了大量PHP-FPM进程，在某些时候会达到PHP-FPM并发子进程数上限（更何况某个会被频繁访问的页面请求涉及该SQL，导致情况更糟），PHP-FPM无法处理新的请求，对于已有的请求也会因为超时导致Nginx响应<span class="number">502</span>。</span><br><span class="line"></span><br><span class="line">那么针对该猜测，可以做两个优化来解决故障：</span><br><span class="line"></span><br><span class="line">优化这条SQL</span><br><span class="line">使用缓存</span><br><span class="line">这条SQL的完整语句为： SELECT h.host,p.result,p.update_time FROM Pixiu p join Host h using(host_id) WHERE result!=<span class="string">'[]'</span> order by update_time desc ，其中字段p.result的类型为 mediumtext NOT NULL ，字段p.update_time的类型为 timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 。</span><br><span class="line"></span><br><span class="line">由于业务逻辑并不要求该SQL的结果是排序的，所以我们将该SQL中的排序条件order by update_time desc删除，经测试发现查询时间大幅度降低到<span class="number">9</span>ms左右（原来的平均查询时间为<span class="number">600</span>多<span class="number">-700</span>ms左右），另外，由于业务逻辑对于该条SQL涉及的数据的实时性要求不高，我们使用Memcached缓存了该SQL的查询结果。</span><br><span class="line">http:<span class="comment">//blog.xiayf.cn/2015/10/02/note-of-a-system-fault/</span></span><br><span class="line"></span><br><span class="line">数据库管理后台默认是只读：读数据表列表、数据表结构、单个表的若干条数据</span><br><span class="line">我们应用在实现上有这样的逻辑：登录用户的每次访问需要登录权限的页面都会自动更新用户的最新的访问时间，即Users数据表的updated_time字段，也即会写Users数据表。</span><br><span class="line">由于磁盘已满，所以写会失败，故障信息提示“数据库中找不到Users数据表”，估计和MySQL的写实现有关。</span><br><span class="line"></span><br><span class="line">之后清理了磁盘，故障立即恢复。</span><br></pre></td></tr></table></figure>
<h3 id="api频率限制"><a href="#api频率限制" class="headerlink" title="api频率限制"></a>api频率限制</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 每个IP一分钟10次</span></span><br><span class="line">$limit = <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">$cache = <span class="keyword">new</span> Memcached();</span><br><span class="line">$cache-&gt;addServer(<span class="string">'127.0.0.1'</span>, <span class="number">11211</span>);</span><br><span class="line"></span><br><span class="line">$key = __FUNCTION__.$_SERVER[<span class="string">'REMOTE_ADDR'</span>];</span><br><span class="line">$requestNum = $cache-&gt;get($key);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> ($requestNum !== FALSE &amp;&amp; $requestNum &gt; <span class="number">10</span>) &#123;</span><br><span class="line">    echo json_encode(array(</span><br><span class="line">        <span class="string">'code'</span> =&gt; <span class="number">403</span>,</span><br><span class="line">        <span class="string">'message'</span> =&gt; <span class="string">'请求太频繁，请一分钟后再试'</span>,</span><br><span class="line">    ));</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$cache-&gt;add($key, <span class="number">0</span>, time()+<span class="number">60</span>);</span><br><span class="line">$cache-&gt;increment($key, <span class="number">1</span>);</span><br><span class="line">http:<span class="comment">//blog.xiayf.cn/2016/06/05/frequency-limitation/</span></span><br></pre></td></tr></table></figure>
<h3 id="curl请求https"><a href="#curl请求https" class="headerlink" title="curl请求https"></a>curl请求https</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">$ curl <span class="string">'https://x.x.x.x/get_ips'</span></span><br><span class="line"></span><br><span class="line">curl: (<span class="number">60</span>) SSL certificate problem, verify that the CA cert is OK. Details:</span><br><span class="line">error:<span class="number">14090086</span>:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed</span><br><span class="line">More details here: http:<span class="comment">//curl.haxx.se/docs/sslcerts.html</span></span><br><span class="line"></span><br><span class="line">发现可能是https证书的问题。于是添加--verbose参数，再次使用curl进行请求，以获取更多交互信息。</span><br><span class="line"></span><br><span class="line">截取部分输出如下</span><br><span class="line"></span><br><span class="line">$ curl <span class="string">'https://x.x.x.x/get_ips'</span> --verbose</span><br><span class="line"></span><br><span class="line">* About to connect() to x.x.x.x port <span class="number">80</span></span><br><span class="line">*   Trying x.x.x.x... connected</span><br><span class="line">* Connected to x.x.x.x (x.x.x.x) port <span class="number">80</span></span><br><span class="line">* successfully set certificate verify locations:</span><br><span class="line">*   CAfile: <span class="regexp">/etc/</span>pki/tls/certs/ca-bundle.crt</span><br><span class="line">CApath: none</span><br><span class="line">* SSLv2, Client hello (<span class="number">1</span>):</span><br><span class="line">SSLv3, TLS handshake, Server hello (<span class="number">2</span>):</span><br><span class="line">SSLv3, TLS handshake, CERT (<span class="number">11</span>):</span><br><span class="line">SSLv3, TLS alert, Server hello (<span class="number">2</span>):</span><br><span class="line">SSL certificate problem, verify that the CA cert is OK. Details:</span><br><span class="line">error:<span class="number">14090086</span>:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed</span><br><span class="line">* Closing connection #0</span><br><span class="line">curl: (<span class="number">60</span>) SSL certificate problem, verify that the CA cert is OK. Details:</span><br><span class="line">error:<span class="number">14090086</span>:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed</span><br><span class="line">More details here: http:<span class="comment">//curl.haxx.se/docs/sslcerts.html</span></span><br><span class="line">可见使用的证书的是/etc/pki/tls/certs/ca-bundle.crt。</span><br><span class="line"></span><br><span class="line">使用测试机上的证书替换线上服务器的证书后，问题解决。</span><br><span class="line"></span><br><span class="line">如果没有可用的证书，可以使用如下方法：</span><br><span class="line"></span><br><span class="line">$ curl http:<span class="comment">//curl.haxx.se/ca/cacert.pem -o /etc/pki/tls/certs/ca-bundle.crt</span></span><br><span class="line">请求https的资源时，遇到证书不匹配的问题，一般的工具都有不进行https证书验证的选项，比如：</span><br><span class="line"></span><br><span class="line">$ wget <span class="string">'https://x.x.x.x/get_ips'</span> --no-check-certificate</span><br><span class="line">$ curl <span class="string">'https://x.x.x.x/get_ips'</span> -k</span><br><span class="line">当然，也可以在请求时指定证书，或者对使用的https ca证书进行更新</span><br><span class="line">http:<span class="comment">//blog.gaoyuan.xyz/2014/05/14/a-trouble-in-request-https-in-curl/</span></span><br></pre></td></tr></table></figure>
<h3 id="pip-error-command-‘gcc’-failed-with-exit-status"><a href="#pip-error-command-‘gcc’-failed-with-exit-status" class="headerlink" title="pip error: command ‘gcc’ failed with exit status"></a>pip error: command ‘gcc’ failed with exit status</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">Command /usr/bin/python -c <span class="string">"import setuptools;__file__='/tmp/pip-build-root/lxml/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))"</span> install --record /tmp/pip-PjviBq-record/install-record.txt --single-version-externally-managed failed <span class="keyword">with</span> error code <span class="number">1</span> <span class="keyword">in</span> /tmp/pip-build-root/lxml</span><br><span class="line">Storing complete log <span class="keyword">in</span> /root/.pip/pip.log</span><br><span class="line">For python <span class="number">2.7</span></span><br><span class="line"></span><br><span class="line">$ sudo yum -y install gcc gcc-c++ kernel-devel</span><br><span class="line">$ sudo yum -y install python-devel libxslt-devel libffi-devel openssl-devel</span><br><span class="line">$ pip install <span class="string">"your python packet"</span></span><br><span class="line">For python <span class="number">3.4</span></span><br><span class="line"></span><br><span class="line">$ sudo apt-get install python3-dev</span><br><span class="line">$ pip install rdbtools python-lzf</span><br><span class="line">https:<span class="comment">//learnku.com/articles/33211</span></span><br></pre></td></tr></table></figure>
<p><a href="https://segmentfault.com/a/1190000002686153" target="_blank" rel="noopener">Nginx 中 502 和 504 错误详解</a></p>
<p><a href="https://github.com/codcodog/Blog/issues/43" target="_blank" rel="noopener">面试题</a></p>
<p><a href="https://github.com/codcodog/Blog/issues/35" target="_blank" rel="noopener">把SESSION存储在数据库中</a></p>
<p><a href="https://www.jianshu.com/p/8555ce285375" target="_blank" rel="noopener">PHP超时的坑</a></p>
<p><a href="http://blog.51cto.com/nanchunle/1657410" target="_blank" rel="noopener">Nginx常见的错误及解决方法</a></p>
<p><a href="http://www.sangeng.org/blog/detail_521.html" target="_blank" rel="noopener">Laravel Redis 多个进程同时取队列问题详解</a></p>
<p><a href="https://laravel-china.org/articles/3729/use-laravel-queue-to-understand-the-knowledge#627df8" target="_blank" rel="noopener">使用 Laravel Queue 不得不明白的知识 </a></p>
<p><a href="https://www.fanhaobai.com/2017/09/lua-in-redis.html" target="_blank" rel="noopener">Lua在Redis的应用</a></p>
<p><a href="https://ruby-china.org/topics/25908" target="_blank" rel="noopener">Redis 实战之薄荷 timeline 的优化</a></p>
<p><a href="http://blog.51cto.com/daweilang/1922940" target="_blank" rel="noopener">Laravel5.2队列驱动expire参数设置带来的重复执行问题 数据库驱动</a></p>
<p><a href="https://segmentfault.com/a/1190000012561418" target="_blank" rel="noopener">为什么 Laravel 会重复执行同一个队列任务？</a></p>
<p><a href="https://suren1986.in/index.php/archives/44/" target="_blank" rel="noopener">Laravel queue 重复执行脚本的问题</a></p>
<p><a href="https://www.jiangxianli.com/?p=80" target="_blank" rel="noopener">Laravel队列使用中踩的坑,不报错但是队列一直再重试</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/问题/" rel="tag"># 问题</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/12/03/斐波那契数列/" rel="next" title="斐波那契数列">
                <i class="fa fa-chevron-left"></i> 斐波那契数列
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/12/05/php命令行工具/" rel="prev" title="php命令行工具">
                php命令行工具 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
          <div class="comments" id="comments">
             <div id="gitment-container"></div>
         </div>
  




        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏生不惑</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">382</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/sushengbuhuo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mysusheng@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/mysusheng" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://v2ex.com/" title="v2ex" target="_blank">v2ex</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.fanhaobai.com/" title="fanhaobai" target="_blank">fanhaobai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="yuanxuxu" target="_blank">yuanxuxu</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.snail-c.cn/article" title="snail-c" target="_blank">snail-c</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://showcj.com/archives" title="showcj" target="_blank">showcj</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://vultr.aicnm.com/%E6%9C%80%E6%96%B0Vultr%E6%B3%A8%E5%86%8C%E5%8F%8AVPS%E8%B4%AD%E4%B9%B0%E5%9B%BE%E6%96%87%E6%95%99%E7%A8%8B/" title="vultr" target="_blank">vultr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lucissfer.com/" title="lucissfer" target="_blank">lucissfer</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/fdipzone/article/details/79352685" title="傲雪星枫" target="_blank">傲雪星枫</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yoby123.cn/index.php/category/default/" title="小白的分享" target="_blank">小白的分享</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/p/5819995.html" title="PHP攻城狮" target="_blank">PHP攻城狮</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.jiaojie.site/" title="php" target="_blank">php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://sphard.com/archives/" title="sphard" target="_blank">sphard</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="LNMP技术栈笔记" target="_blank">LNMP技术栈笔记</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.coding10.com/" title="学习 Laravel" target="_blank">学习 Laravel</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://shuwoom.com/?page_id=929" title="区块链学习指南" target="_blank">区块链学习指南</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://greenlightt.github.io/archives/" title="greenlightt" target="_blank">greenlightt</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.0php.net/archives/" title="0php" target="_blank">0php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.fordba.com/category/mysql" title="mysql" target="_blank">mysql</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.itcodemonkey.com/" title="程序员" target="_blank">程序员</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yanshuo.me/r/v2ex" title="言说" target="_blank">言说</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.timiguo.com/archive.html" title="提米果的博客" target="_blank">提米果的博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://phpartisan.cn/news/112.html" title="phpartisan" target="_blank">phpartisan</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/" title="飞鸿影" target="_blank">飞鸿影</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.54php.cn/" title="54php" target="_blank">54php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lazyman.vip/" title="营销" target="_blank">营销</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.njphper.com/archives/" title="做人呢最重要的就是开心" target="_blank">做人呢最重要的就是开心</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.h57.pw/" title="php 初心者" target="_blank">php 初心者</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#emoji"><span class="nav-number">1.</span> <span class="nav-text">emoji</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#strtotime"><span class="nav-number">2.</span> <span class="nav-text">strtotime</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#三元运算符"><span class="nav-number">3.</span> <span class="nav-text">三元运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Error-while-reading-line-from-the-server-tcp-127-0-0-1-6379"><span class="nav-number">4.</span> <span class="nav-text">Error while reading line from the server. [tcp://127.0.0.1:6379]</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-empty-函数判断结果为空"><span class="nav-number">5.</span> <span class="nav-text">PHP empty 函数判断结果为空</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-Redis-多个进程同时取队列问题"><span class="nav-number">6.</span> <span class="nav-text">Laravel Redis 多个进程同时取队列问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#队列执行失败之后根本不会执行failed方法"><span class="nav-number">7.</span> <span class="nav-text">队列执行失败之后根本不会执行failed方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php7不支持-preg-replace-e修正符"><span class="nav-number">8.</span> <span class="nav-text">php7不支持 preg_replace e修正符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#内存溢出"><span class="nav-number">9.</span> <span class="nav-text">内存溢出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#git-bash-输入python命令停滞"><span class="nav-number">10.</span> <span class="nav-text">git bash 输入python命令停滞</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#保留两位小数"><span class="nav-number">11.</span> <span class="nav-text">保留两位小数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#后期绑定"><span class="nav-number">12.</span> <span class="nav-text">后期绑定</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#"><span class="nav-number">13.</span> <span class="nav-text">{} + []</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#echo-后发送-header"><span class="nav-number">14.</span> <span class="nav-text">echo 后发送 header</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#isset在php5-6-和php7-0-的一些差异"><span class="nav-number">15.</span> <span class="nav-text">isset在php5.6-和php7.0+的一些差异</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel-中使用-导出-excel-时报错"><span class="nav-number">16.</span> <span class="nav-text">laravel 中使用 导出 excel 时报错</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#gzip-UnicodeDecodeError"><span class="nav-number">17.</span> <span class="nav-text">gzip UnicodeDecodeError</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Intervention-Image-图片写入中文"><span class="nav-number">18.</span> <span class="nav-text">Intervention/Image 图片写入中文</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取某字段修改前的值"><span class="nav-number">19.</span> <span class="nav-text">获取某字段修改前的值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#前一天"><span class="nav-number">20.</span> <span class="nav-text">前一天</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#phpstorm-xdebug"><span class="nav-number">21.</span> <span class="nav-text">phpstorm xdebug</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-for-循环中的陷阱"><span class="nav-number">22.</span> <span class="nav-text">Python for 循环中的陷阱</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#pyinstaller-打包-exe"><span class="nav-number">23.</span> <span class="nav-text">pyinstaller 打包 exe</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php捕获fatal-error"><span class="nav-number">24.</span> <span class="nav-text">php捕获fatal error</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP7开始，含十六进制字符串不再被认为是数字"><span class="nav-number">25.</span> <span class="nav-text">PHP7开始，含十六进制字符串不再被认为是数字</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php-artisan-config-cache"><span class="nav-number">26.</span> <span class="nav-text">php artisan config:cache</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#curl超时"><span class="nav-number">27.</span> <span class="nav-text">curl超时</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cURL-error-28"><span class="nav-number">28.</span> <span class="nav-text">cURL error 28</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#格式化"><span class="nav-number">29.</span> <span class="nav-text">格式化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#unicode"><span class="nav-number">30.</span> <span class="nav-text">unicode</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel更新后读取数据失败"><span class="nav-number">31.</span> <span class="nav-text">laravel更新后读取数据失败</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#批量更新"><span class="nav-number">32.</span> <span class="nav-text">批量更新</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cat-多换行"><span class="nav-number">33.</span> <span class="nav-text">cat 多换行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#grep-and-or"><span class="nav-number">34.</span> <span class="nav-text">grep and or</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#memcached-默认的过期时间则-不能大于-30-天"><span class="nav-number">35.</span> <span class="nav-text">memcached 默认的过期时间则 不能大于 30 天</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#unix-tmp-supervisor-sock-refused-connection"><span class="nav-number">36.</span> <span class="nav-text">unix:///tmp/supervisor.sock refused connection</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Specified-key-was-too-long"><span class="nav-number">37.</span> <span class="nav-text">Specified key was too long</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel-with-查询中只查询个别字段"><span class="nav-number">38.</span> <span class="nav-text">laravel with 查询中只查询个别字段</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP输出A到Z"><span class="nav-number">39.</span> <span class="nav-text">PHP输出A到Z</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#env函数的小坑"><span class="nav-number">40.</span> <span class="nav-text">env函数的小坑</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#任务调度的列表"><span class="nav-number">41.</span> <span class="nav-text">任务调度的列表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#在模型事件中获取某字段修改前的值"><span class="nav-number">42.</span> <span class="nav-text">在模型事件中获取某字段修改前的值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel-SerializesModels"><span class="nav-number">43.</span> <span class="nav-text">laravel SerializesModels</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#composer-require失败"><span class="nav-number">44.</span> <span class="nav-text">composer require失败</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Fatal-error-Class-‘PHPUnit-Framework-TestCase’-not-found"><span class="nav-number">45.</span> <span class="nav-text">Fatal error: Class ‘PHPUnit_Framework_TestCase’ not found</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-的-collection"><span class="nav-number">46.</span> <span class="nav-text">Laravel 的 collection</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用”mews-captcha-2-0”-验证码图片不显示问题"><span class="nav-number">47.</span> <span class="nav-text">使用”mews/captcha:~2.0” 验证码图片不显示问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#在解析外部变量时的一个问题"><span class="nav-number">48.</span> <span class="nav-text">在解析外部变量时的一个问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-with-limit"><span class="nav-number">49.</span> <span class="nav-text">Laravel with () limit</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel-5-5-索引长度超过限制"><span class="nav-number">50.</span> <span class="nav-text">laravel 5.5 索引长度超过限制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cron不执行"><span class="nav-number">51.</span> <span class="nav-text">cron不执行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#composer-update后输出Killed"><span class="nav-number">52.</span> <span class="nav-text">composer update后输出Killed</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Memcached-get-could-not-unserialize-value-no-igbinary-support"><span class="nav-number">53.</span> <span class="nav-text">Memcached::get(): could not unserialize value, no igbinary support</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#nginx-配置问题-FastCGI-sent-in-stderr-“Primary-script-unknown”"><span class="nav-number">54.</span> <span class="nav-text">nginx 配置问题-FastCGI sent in stderr: “Primary script unknown”</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#intervention-iamge下载图片过慢"><span class="nav-number">55.</span> <span class="nav-text">intervention/iamge下载图片过慢</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MySQL由一个双引号引发的血案"><span class="nav-number">56.</span> <span class="nav-text">MySQL由一个双引号引发的血案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#foreach"><span class="nav-number">57.</span> <span class="nav-text">foreach</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#proc-open-打开两个进程"><span class="nav-number">58.</span> <span class="nav-text">proc_open()打开两个进程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MySQL大数据分页"><span class="nav-number">59.</span> <span class="nav-text">MySQL大数据分页</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#每三位加个逗号分割"><span class="nav-number">60.</span> <span class="nav-text">每三位加个逗号分割</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#use-amp-Referance"><span class="nav-number">61.</span> <span class="nav-text">use &amp; Referance</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#判断-Eloqument-模型查询数据结果是否为空"><span class="nav-number">62.</span> <span class="nav-text">判断 Eloqument 模型查询数据结果是否为空</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#requires-ext-pcntl"><span class="nav-number">63.</span> <span class="nav-text">requires ext-pcntl</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#exec函数被禁"><span class="nav-number">64.</span> <span class="nav-text">exec函数被禁</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#curl-用完千万别忘-close"><span class="nav-number">65.</span> <span class="nav-text">curl 用完千万别忘 close</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Memcached-get-could-not-unserialize-value-no-igbinary-support-1"><span class="nav-number">66.</span> <span class="nav-text">Memcached::get(): could not unserialize value, no igbinary support</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-读取-ZIP乱码"><span class="nav-number">67.</span> <span class="nav-text">PHP 读取 ZIP乱码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Memcached-get-could-not-unserialize-value-no-igbinary-support-2"><span class="nav-number">68.</span> <span class="nav-text">Memcached::get()could not unserialize value, no igbinary support</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel-redis-队列重复执行"><span class="nav-number">69.</span> <span class="nav-text">laravel redis 队列重复执行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MySQL导入SQL出现MySQL-server-has-gone-away错误"><span class="nav-number">70.</span> <span class="nav-text">MySQL导入SQL出现MySQL server has gone away错误</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#pdo查询出的数据均自动转换为字符串"><span class="nav-number">71.</span> <span class="nav-text">pdo查询出的数据均自动转换为字符串</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#git-clone克隆速度过慢问题"><span class="nav-number">72.</span> <span class="nav-text">git clone克隆速度过慢问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel中导出excel乱码"><span class="nav-number">73.</span> <span class="nav-text">laravel中导出excel乱码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel中进行PDF导出遇到的问题"><span class="nav-number">74.</span> <span class="nav-text">laravel中进行PDF导出遇到的问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#curl获取数据乱码"><span class="nav-number">75.</span> <span class="nav-text">curl获取数据乱码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#mysql-emoji"><span class="nav-number">76.</span> <span class="nav-text">mysql emoji</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#composer-install、update出错需要Token"><span class="nav-number">77.</span> <span class="nav-text">composer install、update出错需要Token</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MISCONF-Redis-is-configured-to-save-RDB-snapshots"><span class="nav-number">78.</span> <span class="nav-text">MISCONF Redis is configured to save RDB snapshots</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel的坑"><span class="nav-number">79.</span> <span class="nav-text">laravel的坑</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#crontab"><span class="nav-number">80.</span> <span class="nav-text">crontab</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel-读操连接了读库和写库"><span class="nav-number">81.</span> <span class="nav-text">laravel 读操连接了读库和写库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#502-Bad-Gateway"><span class="nav-number">82.</span> <span class="nav-text">502 Bad Gateway</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ffmpeg-command-not-found"><span class="nav-number">83.</span> <span class="nav-text">ffmpeg: command not found</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Warning-simplexml-load-file"><span class="nav-number">84.</span> <span class="nav-text">Warning: simplexml_load_file()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#同时读写一个文件的问题"><span class="nav-number">85.</span> <span class="nav-text">同时读写一个文件的问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#nginx-error-open-“-run-nginx-pid”-failed"><span class="nav-number">86.</span> <span class="nav-text">nginx: [error] open() “/run/nginx.pid” failed</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#502"><span class="nav-number">87.</span> <span class="nav-text">502</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php-a-无法使用"><span class="nav-number">88.</span> <span class="nav-text">php -a 无法使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#json-decode-返回null"><span class="nav-number">89.</span> <span class="nav-text">json_decode()返回null</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#env忽略"><span class="nav-number">90.</span> <span class="nav-text">env忽略</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#exceeded-the-timeout-of-60-seconds"><span class="nav-number">91.</span> <span class="nav-text">exceeded the timeout of 60 seconds</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#set-exception-handler-不再保证收到的一定是-Exception-对象"><span class="nav-number">92.</span> <span class="nav-text">set_exception_handler() 不再保证收到的一定是 Exception 对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二维码生成乱码"><span class="nav-number">93.</span> <span class="nav-text">二维码生成乱码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#yum-Error-rpmdb-open-failed"><span class="nav-number">94.</span> <span class="nav-text">yum Error: rpmdb open failed</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#泄露-env-配置"><span class="nav-number">95.</span> <span class="nav-text">泄露 env 配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel-路由冲突"><span class="nav-number">96.</span> <span class="nav-text">laravel 路由冲突</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#No-‘Access-Control-Allow-Origin’-header-is-present-on-the-requested-resource"><span class="nav-number">97.</span> <span class="nav-text">No ‘Access-Control-Allow-Origin’ header is present on the requested resource</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Git-status中文乱码问题"><span class="nav-number">98.</span> <span class="nav-text">Git status中文乱码问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Cannot-run-program-“svn”"><span class="nav-number">99.</span> <span class="nav-text">Cannot run program “svn”</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#svn比较版本-svn-try-setting-http-chunked-requests-to-auto-or-no"><span class="nav-number">100.</span> <span class="nav-text">svn比较版本 svn try setting http-chunked-requests to auto or no</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ipv6惹的祸"><span class="nav-number">101.</span> <span class="nav-text">ipv6惹的祸</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php-只接受1000个-请求参数"><span class="nav-number">102.</span> <span class="nav-text">php 只接受1000个 请求参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#502-Bad-Gateway错误"><span class="nav-number">103.</span> <span class="nav-text">502 Bad Gateway错误</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字符集转转换失败"><span class="nav-number">104.</span> <span class="nav-text">字符集转转换失败</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP7-MongoDB"><span class="nav-number">105.</span> <span class="nav-text">PHP7 MongoDB</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MISCONF-Redis-is-configured-to-save-RDB-snapshots-1"><span class="nav-number">106.</span> <span class="nav-text">MISCONF Redis is configured to save RDB snapshots</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#mongodb3-6-The-‘cursor’-option-is-required-except-for-aggregate-with-the-explain-argument"><span class="nav-number">107.</span> <span class="nav-text">mongodb3.6 The ‘cursor’ option is required, except for aggregate with the explain argument</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php发送email-多了"><span class="nav-number">108.</span> <span class="nav-text">php发送email 多了!</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PhpSpreadsheet-输出-Excel-时自动在-xls-后面加入-html"><span class="nav-number">109.</span> <span class="nav-text">PhpSpreadsheet 输出 Excel 时自动在 xls 后面加入 html</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Composer-使用镜像出现权限问题"><span class="nav-number">110.</span> <span class="nav-text">Composer 使用镜像出现权限问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#No-query-results-for-model"><span class="nav-number">111.</span> <span class="nav-text">No query results for model</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#composer-Out-of-memory"><span class="nav-number">112.</span> <span class="nav-text">composer Out of memory</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#mysql-慢查询-502"><span class="nav-number">113.</span> <span class="nav-text">mysql 慢查询 502</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#api频率限制"><span class="nav-number">114.</span> <span class="nav-text">api频率限制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#curl请求https"><span class="nav-number">115.</span> <span class="nav-text">curl请求https</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#pip-error-command-‘gcc’-failed-with-exit-status"><span class="nav-number">116.</span> <span class="nav-text">pip error: command ‘gcc’ failed with exit status</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏生不惑</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



<div>
<span id="showDays"></span>

</div>

<span id="busuanzi_container_site_pv">
   总访问量:<span id="busuanzi_value_site_pv"></span>次
</span>



  <span class="post-meta-divider">|</span>



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共773.8k字</span>
</div>
<script>
var birthDay = new Date("11/20/2018");
var now = new Date();
var duration = now.getTime() - birthDay.getTime();
var total= Math.floor(duration / (1000 * 60 * 60 * 24));
document.getElementById("showDays").innerHTML = "本站已运行 "+total+" 天";
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  











<script type="text/javascript">
    (function() {
        // 匿名函数，防止污染全局变量
        var utterances = document.createElement('script');
        utterances.type = 'text/javascript';
        utterances.async = true;
        utterances.setAttribute('issue-term','0')
        utterances.setAttribute('theme','')
        utterances.setAttribute('repo','sushengbuhuo/laravel_ioc_demo')
        utterances.crossorigin = 'anonymous';
        utterances.src = 'https://utteranc.es/client.js';
        // content 是要插入评论的地方
        document.getElementById('gitment-container').appendChild(utterances);
    })();
</script>


  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

  
<script>
  ((window.gitter = {}).chat = {}).options = {
    //room替换成自己的聊天室名称即可，room的名称规则是：username/roomname
    room: 'sushengbuhuo-chat/mychat'
  };
</script>
<script src="https://sidecar.gitter.im/dist/sidecar.v1.js" async defer></script>

</body>
</html>
